From 499f163c31643c3be24b56466bc6f9a6e523791d Mon Sep 17 00:00:00 2001 From: Ajax Davis Date: Fri, 2 Jan 2026 01:04:29 +1000 Subject: [PATCH] fix: improve auth sign-in handling and cookie configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add onSuccess/onError callbacks to signIn.email() for better response handling - Add trustedOrigins configuration for production domain - Configure explicit cookie attributes (sameSite, secure, httpOnly) - Enable session cookie caching for better performance - Handle edge case where signIn returns neither data nor error 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- apps/web/src/app/(auth)/sign-in/page.tsx | 37 ++++++++++++++++++------ apps/web/src/lib/auth.ts | 12 ++++++++ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/apps/web/src/app/(auth)/sign-in/page.tsx b/apps/web/src/app/(auth)/sign-in/page.tsx index d5147dd..4ff83c9 100644 --- a/apps/web/src/app/(auth)/sign-in/page.tsx +++ b/apps/web/src/app/(auth)/sign-in/page.tsx @@ -16,21 +16,40 @@ export default function SignInPage() { setLoading(true); try { - const { data, error } = await signIn.email({ - email, - password, - }); + const result = await signIn.email( + { + email, + password, + }, + { + onSuccess: () => { + // Redirect on successful sign-in + window.location.href = '/dashboard'; + }, + onError: (ctx) => { + console.error('Sign in error:', ctx.error); + setError(ctx.error.message || 'Failed to sign in'); + setLoading(false); + }, + } + ); - if (error) { - console.error('Sign in error:', error); - setError(error.message || 'Failed to sign in'); + // Handle case where no callback was triggered + if (result.error) { + console.error('Sign in error:', result.error); + setError(result.error.message || 'Failed to sign in'); setLoading(false); return; } - if (data) { - // Successfully signed in - redirect to dashboard + // If we have data but onSuccess didn't fire, redirect manually + if (result.data) { window.location.href = '/dashboard'; + } else { + // Neither data nor error - something unexpected happened + console.error('Sign in returned no data or error:', result); + setError('Sign in failed. Please try again.'); + setLoading(false); } } catch (err) { console.error('Sign in exception:', err); diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts index 5917597..62fffcf 100644 --- a/apps/web/src/lib/auth.ts +++ b/apps/web/src/lib/auth.ts @@ -35,6 +35,18 @@ export const auth = betterAuth({ session: { expiresIn: 60 * 60 * 24 * 7, // 7 days updateAge: 60 * 60 * 24, // 1 day + cookieCache: { + enabled: true, + maxAge: 60 * 5, // 5 minutes + }, + }, + trustedOrigins: ['https://tpmjs.com'], + advanced: { + defaultCookieAttributes: { + sameSite: 'lax', + secure: true, + httpOnly: true, + }, }, });