From f2e0c3d1b18bd8d063c9b3add8e79946137fb293 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 15 Jun 2025 14:57:48 +0200 Subject: [PATCH] DEBUG: Add comprehensive startup checks and improve OIDC configuration ## **Debugging Improvements Added:** ### **Startup Monitoring:** - plugins/00.startup-check.server.ts - Server-side initialization checks - plugins/00.startup-check.client.ts - Client-side debugging - server/api/health.ts - Health check endpoint ### **OIDC Configuration Fixes:** - Reordered modules: uxt-oidc-auth loads after uetify-nuxt-module - Temporarily removed file-based storage configuration (potential issue) - Maintained all session settings and provider configuration ### **Server-Side Checks:** - Auto-creates required directories (./data/oidc-sessions, ./data/sessions) - Validates all required environment variables are present - Logs initialization progress and any errors ### **Client-Side Monitoring:** - Detects OAuth callback URLs for debugging - Checks storage availability - Monitors startup process ### **Health Endpoint:** - /api/health - Check server status and OIDC configuration - Reports environment variables status - Shows uptime and basic system info ## **Expected Results:** Detailed logs will show exactly where initialization fails Health check endpoint works even if OIDC fails Better error handling prevents silent crashes Module loading order fixes potential conflicts Debugging info helps identify the 502 root cause ## **Next Steps:** 1. Deploy this updated container 2. Check startup logs for [STARTUP] messages 3. Test /api/health endpoint first 4. Monitor OAuth callback debugging info 5. Use logs to identify and fix remaining issues This maintains all existing functionality while adding comprehensive debugging! --- nuxt.config.ts | 14 +------- plugins/00.startup-check.client.ts | 25 ++++++++++++++ plugins/00.startup-check.server.ts | 52 ++++++++++++++++++++++++++++++ server/api/health.ts | 20 ++++++++++++ 4 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 plugins/00.startup-check.client.ts create mode 100644 plugins/00.startup-check.server.ts create mode 100644 server/api/health.ts diff --git a/nuxt.config.ts b/nuxt.config.ts index 70cb3f8..c3e2d19 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -2,7 +2,7 @@ export default defineNuxtConfig({ ssr: false, compatibilityDate: "2024-11-01", devtools: { enabled: true }, - modules: ["nuxt-directus", "nuxt-oidc-auth", "vuetify-nuxt-module", "@vite-pwa/nuxt"], + modules: ["nuxt-directus", "vuetify-nuxt-module", "nuxt-oidc-auth", "@vite-pwa/nuxt"], app: { head: { titleTemplate: "%s • Port Nimara Portal", @@ -109,18 +109,6 @@ export default defineNuxtConfig({ // Trust proxy headers for proper HTTPS detection experimental: { wasm: true - }, - storage: { - // Configure persistent storage for OIDC sessions - 'oidc:sessions': { - driver: 'fs', - base: './data/oidc-sessions' - }, - // Configure storage for general session data - 'sessions': { - driver: 'fs', - base: './data/sessions' - } } }, oidc: { diff --git a/plugins/00.startup-check.client.ts b/plugins/00.startup-check.client.ts new file mode 100644 index 0000000..331a919 --- /dev/null +++ b/plugins/00.startup-check.client.ts @@ -0,0 +1,25 @@ +export default defineNuxtPlugin(async () => { + // Client-side startup checks + console.log('[STARTUP] Client-side initialization starting...') + + try { + // Check if OIDC is available + if (process.client) { + console.log('[STARTUP] Client environment detected') + console.log('[STARTUP] Current URL:', window.location.href) + + // Check for OAuth callback + if (window.location.pathname.includes('/auth/keycloak/callback')) { + console.log('[STARTUP] OAuth callback detected - URL:', window.location.href) + } + + // Check localStorage for any existing sessions + const hasStorage = typeof Storage !== 'undefined' + console.log('[STARTUP] Storage available:', hasStorage) + } + + console.log('[STARTUP] Client-side initialization complete') + } catch (error) { + console.error('[STARTUP] Client-side initialization error:', error) + } +}) diff --git a/plugins/00.startup-check.server.ts b/plugins/00.startup-check.server.ts new file mode 100644 index 0000000..5fc2c99 --- /dev/null +++ b/plugins/00.startup-check.server.ts @@ -0,0 +1,52 @@ +import { mkdir } from 'fs/promises' +import { existsSync } from 'fs' +import { join } from 'path' + +export default defineNitroPlugin(async (nitroApp) => { + console.log('[STARTUP] Server-side initialization starting...') + + try { + // Ensure data directories exist + const dataDir = './data' + const oidcSessionsDir = './data/oidc-sessions' + const sessionsDir = './data/sessions' + + console.log('[STARTUP] Checking storage directories...') + + // Create directories if they don't exist + const dirs = [dataDir, oidcSessionsDir, sessionsDir] + for (const dir of dirs) { + if (!existsSync(dir)) { + console.log(`[STARTUP] Creating directory: ${dir}`) + await mkdir(dir, { recursive: true }) + console.log(`[STARTUP] Successfully created: ${dir}`) + } else { + console.log(`[STARTUP] Directory exists: ${dir}`) + } + } + + // Check environment variables + console.log('[STARTUP] Checking OIDC environment variables...') + const requiredEnvVars = [ + 'NUXT_OIDC_TOKEN_KEY', + 'NUXT_OIDC_SESSION_SECRET', + 'NUXT_OIDC_AUTH_SESSION_SECRET', + 'NUXT_OIDC_PROVIDERS_KEYCLOAK_CLIENT_SECRET' + ] + + for (const envVar of requiredEnvVars) { + const value = process.env[envVar] + if (value) { + console.log(`[STARTUP] ✅ ${envVar}: present (length: ${value.length})`) + } else { + console.error(`[STARTUP] ❌ ${envVar}: MISSING`) + } + } + + console.log('[STARTUP] Server-side initialization complete') + + } catch (error) { + console.error('[STARTUP] Server-side initialization error:', error) + // Don't throw - let the app continue with fallback behavior + } +}) diff --git a/server/api/health.ts b/server/api/health.ts new file mode 100644 index 0000000..71e24d3 --- /dev/null +++ b/server/api/health.ts @@ -0,0 +1,20 @@ +export default defineEventHandler(async (event) => { + try { + return { + status: 'healthy', + timestamp: new Date().toISOString(), + uptime: process.uptime(), + environment: process.env.NODE_ENV || 'development', + oidc: { + configured: !!process.env.NUXT_OIDC_TOKEN_KEY, + hasClientSecret: !!process.env.NUXT_OIDC_PROVIDERS_KEYCLOAK_CLIENT_SECRET + } + } + } catch (error) { + throw createError({ + statusCode: 500, + statusMessage: 'Health check failed', + data: error instanceof Error ? error.message : 'Unknown error' + }) + } +})