mandant-crm/frontend/composables/useAuth.ts
Tim Stollberg 570f17e8b8 feat(frontend): commit uncommitted settings, custom fields, and import work
This local working tree had accumulated a substantial amount of
finished-but-never-committed frontend work: custom client fields
(replacing the old static ClientFields with a dynamic
ClientCustomFieldCard + useCustomFields), CSV import
(useImport + settings/import page), history type management
(useHistoryTypes + settings/history-types page), an admin-only route
middleware, several new icon components, a shared useApiFetch
composable, and a client detail page restructure ([id].vue ->
[id]/index.vue). None of it had ever been pushed, so production was
running a stale build that was already linking to routes/components
that didn't exist server-side (missing IconSettings, 404s on
/settings/import, etc). This commit brings the repo in line with
local state.
2026-07-18 16:27:19 +07:00

39 lines
978 B
TypeScript

import type { User } from '~/types'
export const useAuth = () => {
const user = useState<User | null>('auth.user', () => null)
const getCsrfCookie = async () => {
await useApiFetch('/sanctum/csrf-cookie')
}
const fetchUser = async (): Promise<void> => {
try {
const response = await useApiFetch<{ data: User }>('/api/user')
user.value = response.data
} catch {
user.value = null
}
}
const login = async (email: string, password: string): Promise<void> => {
await getCsrfCookie()
await useApiFetch('/api/login', {
method: 'POST',
body: { email, password },
})
await fetchUser()
}
const logout = async (): Promise<void> => {
try {
await useApiFetch('/api/logout', { method: 'POST' })
} finally {
// Always clear local state and leave, even if the request failed.
user.value = null
await navigateTo('/login')
}
}
return { user, login, logout, fetchUser }
}