Clean up logging from AuthContext
cgen-65adae07fbff4a948a654e717129605c
This commit is contained in:
parent
7186eb5ca6
commit
6b0f53060a
1 changed files with 5 additions and 24 deletions
|
|
@ -197,55 +197,38 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||||
const hasAuthTokens = () => {
|
const hasAuthTokens = () => {
|
||||||
if (typeof window === "undefined") return false;
|
if (typeof window === "undefined") return false;
|
||||||
const keys = Object.keys(window.localStorage);
|
const keys = Object.keys(window.localStorage);
|
||||||
const authKeys = keys.filter(
|
return keys.some(
|
||||||
(key) =>
|
(key) =>
|
||||||
key.includes("auth-token") ||
|
key.includes("auth-token") ||
|
||||||
(key.includes("sb-") && key.includes("-auth")),
|
(key.includes("sb-") && key.includes("-auth")),
|
||||||
);
|
);
|
||||||
console.log("[AUTH] Auth keys in localStorage:", authKeys);
|
|
||||||
return authKeys.length > 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get initial session with persistence recovery
|
// Get initial session with persistence recovery
|
||||||
const initializeAuth = async () => {
|
const initializeAuth = async () => {
|
||||||
try {
|
try {
|
||||||
console.log("[AUTH] Initializing auth context...");
|
|
||||||
const {
|
const {
|
||||||
data: { session },
|
data: { session },
|
||||||
} = await supabase.auth.getSession();
|
} = 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
|
// If no session but tokens exist, the session might not have restored yet
|
||||||
// Wait a bit for onAuthStateChange to trigger
|
// Wait for onAuthStateChange to trigger
|
||||||
if (!session && hasTokens) {
|
if (!session && hasAuthTokens()) {
|
||||||
console.log(
|
|
||||||
"[AUTH] Tokens exist in storage but session not yet restored, waiting for onAuthStateChange...",
|
|
||||||
);
|
|
||||||
// Don't set loading to false yet - wait for onAuthStateChange
|
// Don't set loading to false yet - wait for onAuthStateChange
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session?.user) {
|
if (session?.user) {
|
||||||
sessionRestored = true;
|
sessionRestored = true;
|
||||||
console.log("[AUTH] Session restored from getSession()");
|
|
||||||
setSession(session);
|
setSession(session);
|
||||||
setUser(session.user);
|
setUser(session.user);
|
||||||
await fetchUserProfile(session.user.id);
|
await fetchUserProfile(session.user.id);
|
||||||
} else {
|
} else {
|
||||||
console.log("[AUTH] No session found, marking as initialized");
|
|
||||||
sessionRestored = true;
|
sessionRestored = true;
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[AUTH] Error getting session:", error);
|
console.error("Error getting session:", error);
|
||||||
sessionRestored = true;
|
sessionRestored = true;
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|
@ -257,17 +240,15 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||||
const {
|
const {
|
||||||
data: { subscription },
|
data: { subscription },
|
||||||
} = supabase.auth.onAuthStateChange(async (event, session) => {
|
} = supabase.auth.onAuthStateChange(async (event, session) => {
|
||||||
console.log("[AUTH] onAuthStateChange event:", event, "User:", session?.user?.email ?? "null");
|
console.log("Auth state change:", event, !!session?.user);
|
||||||
|
|
||||||
sessionRestored = true;
|
sessionRestored = true;
|
||||||
setSession(session);
|
setSession(session);
|
||||||
setUser(session?.user ?? null);
|
setUser(session?.user ?? null);
|
||||||
|
|
||||||
if (session?.user) {
|
if (session?.user) {
|
||||||
console.log("[AUTH] User authenticated, fetching profile...");
|
|
||||||
await fetchUserProfile(session.user.id);
|
await fetchUserProfile(session.user.id);
|
||||||
} else {
|
} else {
|
||||||
console.log("[AUTH] No user, clearing auth state");
|
|
||||||
setProfile(null);
|
setProfile(null);
|
||||||
setRoles([]);
|
setRoles([]);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue