39 lines
892 B
TypeScript
39 lines
892 B
TypeScript
export interface AuthUser {
|
|
username: string | null
|
|
email: string | null
|
|
name: string | null
|
|
uid: string | null
|
|
groups: string[]
|
|
authenticated: boolean
|
|
}
|
|
|
|
export const useAuth = () => {
|
|
const user = useState<AuthUser | null>('auth-user', () => null)
|
|
const loading = useState<boolean>('auth-loading', () => false)
|
|
|
|
const fetchUser = async () => {
|
|
loading.value = true
|
|
try {
|
|
const data = await $fetch<AuthUser>('/api/auth/user')
|
|
user.value = data
|
|
} catch (error) {
|
|
console.error('Error fetching user:', error)
|
|
user.value = null
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const logout = () => {
|
|
// Authentik maneja el logout, redirigir a la URL de logout
|
|
window.location.href = '/outpost.goauthentik.io/sign_out'
|
|
}
|
|
|
|
return {
|
|
user: readonly(user),
|
|
loading: readonly(loading),
|
|
fetchUser,
|
|
logout
|
|
}
|
|
}
|