diff --git a/src/app.ts b/src/app.ts index 037bc52..8e0565b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -123,23 +123,38 @@ export async function createAppInstance() { // Install all enabled modules await pluginManager.installAll() + // Initialize auth before setting up router guards + const { auth } = await import('@/composables/useAuth') + await auth.initialize() + console.log('Auth initialized, isAuthenticated:', auth.isAuthenticated.value) + // Set up auth guard router.beforeEach(async (to, _from, next) => { - // Import the auth composable to ensure we're using the same instance as the rest of the app - const { auth } = await import('@/composables/useAuth') + // Default to requiring auth unless explicitly set to false + const requiresAuth = to.meta.requiresAuth !== false - if (to.meta.requiresAuth && !auth.isAuthenticated.value) { - console.log('Auth guard: User not authenticated, redirecting to login') + if (requiresAuth && !auth.isAuthenticated.value) { + console.log(`Auth guard: User not authenticated, redirecting from ${to.path} to login`) next('/login') } else if (to.path === '/login' && auth.isAuthenticated.value) { console.log('Auth guard: User already authenticated, redirecting to home') next('/') } else { - console.log(`Auth guard: Allowing navigation to ${to.path}`) + console.log(`Auth guard: Allowing navigation to ${to.path} (requiresAuth: ${requiresAuth}, authenticated: ${auth.isAuthenticated.value})`) next() } }) + // Check initial route and redirect if needed + if (!auth.isAuthenticated.value) { + const currentRoute = router.currentRoute.value + const requiresAuth = currentRoute.meta.requiresAuth !== false + if (requiresAuth) { + console.log('Initial route requires auth but user not authenticated, redirecting to login') + await router.push('/login') + } + } + // Global error handling app.config.errorHandler = (err, _vm, info) => { console.error('Global error:', err, info) diff --git a/src/modules/base/index.ts b/src/modules/base/index.ts index 28ea856..d5c62ee 100644 --- a/src/modules/base/index.ts +++ b/src/modules/base/index.ts @@ -50,10 +50,7 @@ export const baseModule: ModulePlugin = { // Initialize core services relayHub.setRelayUrls(options?.config?.nostr?.relays || []) await relayHub.initialize() - await auth.initialize({ - waitForDependencies: false, // Auth has no dependencies - maxRetries: 1 - }) + // Auth initialization moved to app.ts before router guards await paymentService.initialize({ waitForDependencies: true, // PaymentService depends on AuthService maxRetries: 3