Something went wrong. Try again.
Smoking habit tracker
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930// Server-only plugin: rehydrate auth state during SSR from the httpOnly// session cookie, so the first paint already knows the user (and the// login/register guards can redirect without waiting for client restore).//// NOTE: this intentionally calls the /me API over HTTP instead of reading// the DB directly. The previous implementation dynamically imported// `../server/db/schema`, which resolves in dev but NOT in the production// bundle (source files aren't shipped under .output), failing silently and// leaving `auth-token` null on every SSR render.export default defineNuxtPlugin(async () => { const token = useState<import('../composables/useAuth').AuthToken | null>('auth-token', () => null) if (token.value) return
// No browser session cookie on this request → nothing to restore. const { cookie } = useRequestHeaders(['cookie']) if (!cookie) return
try { const me = await $fetch<{ user: { id: number; username: string } }>('/api/v2/auth/me', { headers: { cookie }, }) token.value = { access_token: '', // httpOnly cookie stays hidden from JS; API auth goes via cookie user: me.user, } } catch { // Invalid/expired session — token stays null, pages render logged-out. }})