entorno de desarrollo listo
This commit is contained in:
38
nuxt4-app/app/composables/useAuth.ts
Normal file
38
nuxt4-app/app/composables/useAuth.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user