Files
InvoiceShelf/resources/scripts-v2/composables/use-auth.ts
Darko Gjorgjijoski a46cca5cd8 Complete scripts-v2 TypeScript migration — all imports resolved,
build passes

Create all missing components (modals, dropdowns, icons, tabs, mail
drivers, customer partials), fix all @/scripts/ imports to @v2/,
wire up vite entry point and blade template. 382 files, 48883 lines.

- 27 settings components: modals (tax, payment, custom field, note,
  category, role, exchange rate, unit, mail test), dropdowns (6),
  customization tabs (4), mail driver forms (4)
- 22 icon components: 5 utility icons, 4 dashboard icons, 13 editor
  toolbar icons with typed barrel export
- 3 customer components: info, chart placeholder, custom fields single
- Fixed usePopper composable, client/format-money import patterns
- Zero remaining @/scripts/ imports in scripts-v2/

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 09:30:00 +02:00

87 lines
2.1 KiB
TypeScript

import { ref, computed } from 'vue'
import type { Ref, ComputedRef } from 'vue'
import * as ls from '../utils/local-storage'
import { LS_KEYS } from '@v2/config/constants'
export interface User {
id: number
name: string
email: string
avatar: string | number
is_owner: boolean
is_super_admin: boolean
[key: string]: unknown
}
export interface UseAuthReturn {
currentUser: Ref<User | null>
isAuthenticated: ComputedRef<boolean>
isOwner: ComputedRef<boolean>
isSuperAdmin: ComputedRef<boolean>
setUser: (user: User) => void
clearUser: () => void
login: (loginFn: () => Promise<User>) => Promise<User>
logout: (logoutFn: () => Promise<void>) => Promise<void>
}
const currentUser = ref<User | null>(null)
/**
* Composable for managing authentication state.
* Provides the current user, login/logout helpers, and role-based computed properties.
*/
export function useAuth(): UseAuthReturn {
const isAuthenticated = computed<boolean>(() => currentUser.value !== null)
const isOwner = computed<boolean>(
() => currentUser.value?.is_owner === true
)
const isSuperAdmin = computed<boolean>(
() => currentUser.value?.is_super_admin === true
)
function setUser(user: User): void {
currentUser.value = user
}
function clearUser(): void {
currentUser.value = null
}
/**
* Execute a login function and set the current user on success.
*
* @param loginFn - Async function that performs the login and returns a User
* @returns The authenticated user
*/
async function login(loginFn: () => Promise<User>): Promise<User> {
const user = await loginFn()
currentUser.value = user
return user
}
/**
* Execute a logout function and clear auth state.
*
* @param logoutFn - Async function that performs the logout
*/
async function logout(logoutFn: () => Promise<void>): Promise<void> {
await logoutFn()
currentUser.value = null
ls.remove(LS_KEYS.AUTH_TOKEN)
ls.remove(LS_KEYS.SELECTED_COMPANY)
}
return {
currentUser,
isAuthenticated,
isOwner,
isSuperAdmin,
setUser,
clearUser,
login,
logout,
}
}