Skip to content

Commit e3476ea

Browse files
prazgaitisclaude
andcommitted
Fix mobile auth reliability: disable refetch-on-focus, enable session cache, fix CORS
- Disable refetchOnWindowFocus in Better Auth client to prevent mobile tab-switch visibility changes from clearing auth state mid-session - Enable server-side session cookieCache (5 min TTL) to avoid DB lookups on every /get-session call, speeding up auth bootstrap on slow connections - Add www variant to CORS allowedOrigins in http.ts to match trustedOrigins in auth.ts — prevents CORS failures when accessing via www subdomain - Add console.error logging to silent catch blocks in getCurrentUser and getServerAuth so auth failures are visible in production logs Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2d8a746 commit e3476ea

5 files changed

Lines changed: 20 additions & 4 deletions

File tree

apps/web/lib/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export const getCurrentUser = cache(async function getCurrentUser(): Promise<Doc
2828
{},
2929
);
3030
}
31-
} catch {
32-
// Auth query/mutation failed - user will remain null
31+
} catch (error) {
32+
console.error("[auth] getCurrentUser failed:", error);
3333
}
3434

3535
return user;

apps/web/lib/better-auth/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ export const betterAuthClient = createAuthClient({
2424
credentials: "include",
2525
},
2626
plugins: [convexClient()],
27+
sessionOptions: {
28+
// Disable automatic refetch on tab focus — on mobile, switching apps or
29+
// pulling down the notification shade triggers visibilitychange, causing
30+
// unnecessary /get-session round-trips that temporarily clear auth state.
31+
refetchOnWindowFocus: false,
32+
},
2733
});

apps/web/lib/server-auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ export async function getServerAuth(): Promise<ServerAuthResult> {
108108
},
109109
};
110110
}
111-
} catch {
112-
// Query/mutation failed, fall through to unauthenticated response.
111+
} catch (error) {
112+
console.error("[server-auth] getServerAuth failed:", error);
113113
}
114114

115115
return {

packages/backend/auth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export const createAuth = (ctx: GenericCtx<DataModel>) => {
3939
emailAndPassword: {
4040
enabled: true,
4141
},
42+
session: {
43+
cookieCache: {
44+
enabled: true,
45+
maxAge: 5 * 60, // 5 minutes — avoids DB lookup on every /get-session
46+
},
47+
},
4248
socialProviders: {
4349
google: {
4450
clientId: process.env.GOOGLE_CLIENT_ID!,

packages/backend/http.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ authComponent.registerRoutes(http, createAuth, {
3535
"http://localhost:3000",
3636
"http://localhost:3001",
3737
process.env.SITE_URL || "",
38+
// Also allow www variant (matches trustedOrigins in auth.ts)
39+
...(process.env.SITE_URL
40+
? [process.env.SITE_URL.replace("://", "://www.")]
41+
: []),
3842
].filter(Boolean),
3943
},
4044
});

0 commit comments

Comments
 (0)