- Define useAuth composable logic with syncUser, handleLoginSuccess, and logout methods - Add auth.ts and guest.ts route middlewares to guard routes based on authentication and user roles - Add auth.client.ts plugin to boot-sync logged in user details - Declare custom PageMeta type definitions to support role-based authorization rules
17 lines
426 B
TypeScript
17 lines
426 B
TypeScript
export default defineNuxtPlugin(async () => {
|
|
const auth = useAuth()
|
|
|
|
if ((auth.token.value || auth.tokenState.value) && !auth.user.value) {
|
|
try {
|
|
await auth.syncUser()
|
|
} catch (error) {
|
|
console.error('[Auth Plugin] Sync failed:', error)
|
|
auth.logout()
|
|
}
|
|
}
|
|
|
|
console.log('[Auth Plugin] Initialized - User:', auth.user.value?.id || 'None', 'Authenticated:', auth.isAuthenticated.value)
|
|
})
|
|
|
|
|