Skip to content

Commit a9e8cf6

Browse files
WangYuTenggclaude
andcommitted
fix(auth): stop password-login session being wiped on initial mount
Two bugs in onAuthStateChange that bounced password-authed users to /login when they navigated directly to a deep URL: - The handler unconditionally cleared dbUser when Supabase had no session, even if a custom JWT (password login) was active. - The handler unconditionally flipped loading=false on every Supabase event, including the initial INITIAL_SESSION emitted before the custom-JWT fetchDbUser had finished. Route guards then ran with loading=false and dbUser=null and redirected. Now: only clear dbUser if there is no custom token, and let the custom-token path own setLoading(false) when it's the active session. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 229c6de commit a9e8cf6

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/client/contexts/AuthContext.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ export function AuthProvider({ children }: { children: ReactNode }) {
7878
} = supabase.auth.onAuthStateChange((_event: string, session: Session | null) => {
7979
setSession(session);
8080
setUser(session?.user ?? null);
81-
81+
82+
const hasCustomToken = !!localStorage.getItem(CUSTOM_TOKEN_KEY);
83+
8284
if (session?.access_token) {
8385
fetchDbUser(session.access_token);
84-
} else {
86+
setLoading(false);
87+
} else if (!hasCustomToken) {
88+
// No Supabase session and no custom JWT → fully signed out.
8589
setDbUser(null);
90+
setLoading(false);
8691
}
87-
88-
setLoading(false);
92+
// else: custom-JWT login is in flight; let its fetchDbUser.finally flip loading.
8993
});
9094

9195
return () => subscription.unsubscribe();

0 commit comments

Comments
 (0)