mirror of
https://github.com/Luchexx/wacrm.git
synced 2026-11-02 09:19:51 +00:00
Merge pull request #6 from ArnasDon/fix/use-auth-getsession
fix: use getSession() in useAuth hook to stop 10s timeout
This commit is contained in:
commit
7cc6a840d5
@ -20,13 +20,14 @@ export function useAuth() {
|
|||||||
const supabase = createClient();
|
const supabase = createClient();
|
||||||
let mounted = true;
|
let mounted = true;
|
||||||
|
|
||||||
// Safety net: if something hangs, surface it instead of an infinite spinner.
|
// Safety net: 3s is plenty for localStorage reads. If it hangs here,
|
||||||
|
// something else is wrong (likely storage permissions in an iframe).
|
||||||
const safetyTimer = setTimeout(() => {
|
const safetyTimer = setTimeout(() => {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
console.warn("[useAuth] getUser() timed out after 10s, clearing loading state");
|
console.warn("[useAuth] getSession() timed out after 3s, clearing loading state");
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
}, 10000);
|
}, 3000);
|
||||||
|
|
||||||
const fetchProfile = async (userId: string) => {
|
const fetchProfile = async (userId: string) => {
|
||||||
try {
|
try {
|
||||||
@ -57,23 +58,25 @@ export function useAuth() {
|
|||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
try {
|
try {
|
||||||
|
// getSession() reads from localStorage — instant, no network call.
|
||||||
|
// The middleware already validates the JWT server-side with getUser()
|
||||||
|
// on every request, and RLS enforces authorization at the DB level,
|
||||||
|
// so the client can trust the local session for UI purposes.
|
||||||
const {
|
const {
|
||||||
data: { user },
|
data: { session },
|
||||||
error,
|
error,
|
||||||
} = await supabase.auth.getUser();
|
} = await supabase.auth.getSession();
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
// AuthSessionMissingError is expected when not logged in — don't log as error
|
console.error("[useAuth] getSession error:", error.message);
|
||||||
if (error.name !== "AuthSessionMissingError") {
|
|
||||||
console.error("[useAuth] getUser error:", error.message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setUser(user);
|
const currentUser = session?.user ?? null;
|
||||||
|
setUser(currentUser);
|
||||||
|
|
||||||
if (user) {
|
if (currentUser) {
|
||||||
await fetchProfile(user.id);
|
await fetchProfile(currentUser.id);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("[useAuth] init threw:", err);
|
console.error("[useAuth] init threw:", err);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user