mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-18 18:54:07 +00:00
Phase 1: TypeScript foundation in scripts-v2/
Create the complete TypeScript foundation for the Vue 3 migration in a parallel scripts-v2/ directory. 72 files, 5430 lines, zero any types, strict mode. - types/ (21 files): Domain interfaces for all 17 entities derived from actual Laravel models and API resources. Enums for all statuses. Generic API response wrappers. - api/ (29 files): Typed axios client with interceptors, endpoint constants from routes/api.php, 25 typed service classes covering every API endpoint. - composables/ (14 files): Vue 3 composition functions for auth, notifications, dialogs, modals, pagination, filters, currency, dates, theme, sidebar, company context, and permissions. - utils/ (5 files): Pure typed utilities for money formatting, date formatting (date-fns), localStorage, and error handling. - config/ (3 files): Typed ability constants, app constants. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
86
resources/scripts-v2/composables/use-auth.ts
Normal file
86
resources/scripts-v2/composables/use-auth.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { ref, computed } from 'vue'
|
||||
import type { Ref, ComputedRef } from 'vue'
|
||||
import * as ls from '../utils/local-storage'
|
||||
import { LS_KEYS } from '../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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user