mirror of
https://github.com/Luchexx/wacrm.git
synced 2026-11-02 09:19:51 +00:00
Merge pull request #4 from ArnasDon/fix/auth-hook-infinite-loading
Fix: dashboard stuck on infinite 'Loading...' spinner
This commit is contained in:
commit
928a67e12b
@ -16,43 +16,79 @@ export function useAuth() {
|
|||||||
const [profile, setProfile] = useState<Profile | null>(null);
|
const [profile, setProfile] = useState<Profile | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
const supabase = createClient();
|
const supabase = createClient();
|
||||||
|
let mounted = true;
|
||||||
|
|
||||||
const fetchProfile = useCallback(
|
// Safety net: if something hangs, surface it instead of an infinite spinner.
|
||||||
async (userId: string) => {
|
const safetyTimer = setTimeout(() => {
|
||||||
const { data } = await supabase
|
if (mounted) {
|
||||||
|
console.warn("[useAuth] getUser() timed out after 10s, clearing loading state");
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, 10000);
|
||||||
|
|
||||||
|
const fetchProfile = async (userId: string) => {
|
||||||
|
try {
|
||||||
|
// profiles.user_id (NOT profiles.id) references auth.users.id
|
||||||
|
const { data, error } = await supabase
|
||||||
.from("profiles")
|
.from("profiles")
|
||||||
.select("id, full_name, email, avatar_url")
|
.select("id, full_name, email, avatar_url")
|
||||||
.eq("id", userId)
|
.eq("user_id", userId)
|
||||||
.single();
|
.maybeSingle();
|
||||||
|
|
||||||
if (data) {
|
if (error) {
|
||||||
|
console.error("[useAuth] fetchProfile error:", {
|
||||||
|
message: error.message,
|
||||||
|
details: error.details,
|
||||||
|
hint: error.hint,
|
||||||
|
code: error.code,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data && mounted) {
|
||||||
setProfile(data);
|
setProfile(data);
|
||||||
}
|
}
|
||||||
},
|
} catch (err) {
|
||||||
[supabase]
|
console.error("[useAuth] fetchProfile threw:", err);
|
||||||
);
|
}
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
const init = async () => {
|
||||||
const getUser = async () => {
|
try {
|
||||||
const {
|
const {
|
||||||
data: { user },
|
data: { user },
|
||||||
|
error,
|
||||||
} = await supabase.auth.getUser();
|
} = await supabase.auth.getUser();
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
// AuthSessionMissingError is expected when not logged in — don't log as error
|
||||||
|
if (error.name !== "AuthSessionMissingError") {
|
||||||
|
console.error("[useAuth] getUser error:", error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mounted) return;
|
||||||
setUser(user);
|
setUser(user);
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
await fetchProfile(user.id);
|
await fetchProfile(user.id);
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
setLoading(false);
|
console.error("[useAuth] init threw:", err);
|
||||||
|
} finally {
|
||||||
|
if (mounted) setLoading(false);
|
||||||
|
clearTimeout(safetyTimer);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
getUser();
|
init();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: { subscription },
|
data: { subscription },
|
||||||
} = supabase.auth.onAuthStateChange(async (event, session) => {
|
} = supabase.auth.onAuthStateChange(async (_event, session) => {
|
||||||
|
if (!mounted) return;
|
||||||
const currentUser = session?.user ?? null;
|
const currentUser = session?.user ?? null;
|
||||||
setUser(currentUser);
|
setUser(currentUser);
|
||||||
|
|
||||||
@ -66,16 +102,21 @@ export function useAuth() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
mounted = false;
|
||||||
|
clearTimeout(safetyTimer);
|
||||||
subscription.unsubscribe();
|
subscription.unsubscribe();
|
||||||
};
|
};
|
||||||
}, [supabase, fetchProfile]);
|
// Intentionally run once on mount — createClient() is a singleton
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, []);
|
||||||
|
|
||||||
const signOut = useCallback(async () => {
|
const signOut = useCallback(async () => {
|
||||||
|
const supabase = createClient();
|
||||||
await supabase.auth.signOut();
|
await supabase.auth.signOut();
|
||||||
setUser(null);
|
setUser(null);
|
||||||
setProfile(null);
|
setProfile(null);
|
||||||
window.location.href = "/login";
|
window.location.href = "/login";
|
||||||
}, [supabase]);
|
}, []);
|
||||||
|
|
||||||
return { user, profile, loading, signOut };
|
return { user, profile, loading, signOut };
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user