From bb15d61da72a663fe2e4f2b5666663a94a119fa8 Mon Sep 17 00:00:00 2001 From: JacobLinCool Date: Mon, 24 Feb 2025 06:04:04 -0800 Subject: [PATCH] fix: auth bug (#150) --- src/hooks.server.ts | 1 + src/lib/components/Auth.svelte | 2 +- src/lib/stores/auth.ts | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 97635d5..c1e0c78 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -25,6 +25,7 @@ const authHandle: Handle = async ({ event, resolve }) => { if ( event.url.pathname.startsWith('/api') && !event.url.pathname.startsWith('/api/auth/signin') && + !event.url.pathname.startsWith('/api/auth/signout') && !event.locals.user ) { return new Response(JSON.stringify({ error: 'Unauthorized' }), { diff --git a/src/lib/components/Auth.svelte b/src/lib/components/Auth.svelte index 5d5d4b4..f08969e 100644 --- a/src/lib/components/Auth.svelte +++ b/src/lib/components/Auth.svelte @@ -5,7 +5,7 @@ {#if $user} -
+

Welcome, {$profile?.displayName || $user.displayName}!

diff --git a/src/lib/stores/auth.ts b/src/lib/stores/auth.ts index 69a7798..42865ab 100644 --- a/src/lib/stores/auth.ts +++ b/src/lib/stores/auth.ts @@ -10,6 +10,7 @@ export const user = writable(null); // Listen to auth state changes auth.onAuthStateChanged((newUser) => { + console.log('Auth state changed:', newUser); user.set(newUser); }); @@ -45,7 +46,9 @@ export async function signInWithGoogle() { // Sign out function export async function signOut(f: typeof fetch = fetch) { try { - await auth.signOut(); + if (auth.currentUser) { + await auth.signOut(); + } // Clear the session cookie await f('/api/auth/signout', { method: 'POST' }); // Redirect to home page