Add detailed logging to diagnose session persistence
cgen-57a779f645c946618b4c383c0aef73f9
This commit is contained in:
parent
38f1a99326
commit
14a027fbb1
1 changed files with 18 additions and 4 deletions
|
|
@ -197,25 +197,37 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
|||
const hasAuthTokens = () => {
|
||||
if (typeof window === "undefined") return false;
|
||||
const keys = Object.keys(window.localStorage);
|
||||
return keys.some(
|
||||
const authKeys = keys.filter(
|
||||
(key) =>
|
||||
key.includes("auth-token") ||
|
||||
(key.includes("sb-") && key.includes("-auth")),
|
||||
);
|
||||
console.log("[AUTH] Auth keys in localStorage:", authKeys);
|
||||
return authKeys.length > 0;
|
||||
};
|
||||
|
||||
// Get initial session with persistence recovery
|
||||
const initializeAuth = async () => {
|
||||
try {
|
||||
console.log("[AUTH] Initializing auth context...");
|
||||
const {
|
||||
data: { session },
|
||||
} = await supabase.auth.getSession();
|
||||
|
||||
console.log(
|
||||
"[AUTH] getSession() returned:",
|
||||
session?.user ? `User: ${session.user.email}` : "No session",
|
||||
);
|
||||
|
||||
// Check localStorage directly
|
||||
const hasTokens = hasAuthTokens();
|
||||
console.log("[AUTH] Tokens exist in storage:", hasTokens);
|
||||
|
||||
// If no session but tokens exist, the session might not have restored yet
|
||||
// Wait a bit for onAuthStateChange to trigger
|
||||
if (!session && hasAuthTokens()) {
|
||||
if (!session && hasTokens) {
|
||||
console.log(
|
||||
"Tokens exist in storage but session not yet restored, waiting...",
|
||||
"[AUTH] Tokens exist in storage but session not yet restored, waiting for onAuthStateChange...",
|
||||
);
|
||||
// Don't set loading to false yet - wait for onAuthStateChange
|
||||
return;
|
||||
|
|
@ -223,15 +235,17 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
|||
|
||||
if (session?.user) {
|
||||
sessionRestored = true;
|
||||
console.log("[AUTH] Session restored from getSession()");
|
||||
setSession(session);
|
||||
setUser(session.user);
|
||||
await fetchUserProfile(session.user.id);
|
||||
} else {
|
||||
console.log("[AUTH] No session found, marking as initialized");
|
||||
sessionRestored = true;
|
||||
setLoading(false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error getting session:", error);
|
||||
console.error("[AUTH] Error getting session:", error);
|
||||
sessionRestored = true;
|
||||
setLoading(false);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue