28 lines
755 B
TypeScript
28 lines
755 B
TypeScript
// plugins/auth.client.ts
|
||
// This plugin initializes authentication state once on app load
|
||
// Runs only on client-side before any components mount
|
||
|
||
export default defineNuxtPlugin({
|
||
name: 'auth',
|
||
enforce: 'pre', // Run before other plugins
|
||
async setup(nuxtApp) {
|
||
|
||
const { checkAuth, user } = useAuth();
|
||
|
||
try {
|
||
// Initialize auth state once on app load
|
||
// This MUST complete before app renders
|
||
const userData = await checkAuth();
|
||
|
||
if (userData) {
|
||
console.log('✅ Auth plugin: User authenticated');
|
||
} else {
|
||
console.log('ℹ️ Auth plugin: No active session');
|
||
}
|
||
} catch (err) {
|
||
console.error('❌ Auth plugin: Failed to initialize auth:', err);
|
||
}
|
||
|
||
}
|
||
});
|