Spaces:
Sleeping
Sleeping
fix(nav): rewrite auth logic — set loaded=true on SIGNED_IN immediately, fetch profile in background
Browse files- web/components/nav.tsx +90 -67
web/components/nav.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
| 8 |
ScanText, Settings, LogIn, Zap, Sparkles, CreditCard, UserCircle,
|
| 9 |
LogOut, Users
|
| 10 |
} from "lucide-react";
|
| 11 |
-
import { useState, useEffect
|
| 12 |
import { createClient } from "@/lib/supabase/client";
|
| 13 |
|
| 14 |
interface NavLink {
|
|
@@ -28,80 +28,103 @@ export function Nav() {
|
|
| 28 |
const isAdmin = userRole === "admin";
|
| 29 |
const hasTeam = !!userTeam;
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
setUserEmail(null);
|
| 41 |
-
setUserRole(null);
|
| 42 |
-
setUserTeam(null);
|
| 43 |
-
setLoaded(true);
|
| 44 |
-
return;
|
| 45 |
-
}
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
-
|
| 66 |
-
// Supabase client init error (env vars missing)
|
| 67 |
}
|
| 68 |
-
setLoaded(true);
|
| 69 |
-
}, []);
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
if (event === "SIGNED_IN" || event === "SIGNED_OUT" || event === "TOKEN_REFRESHED" || event === "INITIAL_SESSION") {
|
| 86 |
-
refreshAuth();
|
| 87 |
}
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
return () => {
|
| 95 |
-
|
|
|
|
| 96 |
};
|
| 97 |
-
}, [
|
| 98 |
-
|
| 99 |
-
// FIX v4.3: Also re-check when pathname changes (covers router.push after login)
|
| 100 |
-
useEffect(() => {
|
| 101 |
-
if (loaded) {
|
| 102 |
-
refreshAuth();
|
| 103 |
-
}
|
| 104 |
-
}, [pathname, loaded, refreshAuth]);
|
| 105 |
|
| 106 |
async function handleSignOut() {
|
| 107 |
try {
|
|
|
|
| 8 |
ScanText, Settings, LogIn, Zap, Sparkles, CreditCard, UserCircle,
|
| 9 |
LogOut, Users
|
| 10 |
} from "lucide-react";
|
| 11 |
+
import { useState, useEffect } from "react";
|
| 12 |
import { createClient } from "@/lib/supabase/client";
|
| 13 |
|
| 14 |
interface NavLink {
|
|
|
|
| 28 |
const isAdmin = userRole === "admin";
|
| 29 |
const hasTeam = !!userTeam;
|
| 30 |
|
| 31 |
+
useEffect(() => {
|
| 32 |
+
let cancelled = false;
|
| 33 |
+
const supabase = createClient();
|
| 34 |
+
|
| 35 |
+
async function checkAuth() {
|
| 36 |
+
try {
|
| 37 |
+
const { data: { session } } = await supabase.auth.getSession();
|
| 38 |
+
|
| 39 |
+
if (cancelled) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
if (!session?.user) {
|
| 42 |
+
setUserEmail(null);
|
| 43 |
+
setUserRole(null);
|
| 44 |
+
setUserTeam(null);
|
| 45 |
+
if (!loaded) setLoaded(true);
|
| 46 |
+
return;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
const user = session.user;
|
| 50 |
+
setUserEmail(user.email || null);
|
| 51 |
+
|
| 52 |
+
// Fetch profile for role/team — use getSession instead of getUser
|
| 53 |
+
// to avoid extra network call
|
| 54 |
+
const { data: profile, error: profileError } = await supabase
|
| 55 |
+
.from("profiles")
|
| 56 |
+
.select("role, team_id")
|
| 57 |
+
.eq("id", user.id)
|
| 58 |
+
.single();
|
| 59 |
+
|
| 60 |
+
if (cancelled) return;
|
| 61 |
+
|
| 62 |
+
if (profileError) {
|
| 63 |
+
console.error("[ClauseGuard Nav] Profile error:", profileError.message);
|
| 64 |
+
// Even if profile fails, still show the user as logged in
|
| 65 |
+
setUserRole("user");
|
| 66 |
+
setUserTeam(null);
|
| 67 |
+
} else {
|
| 68 |
+
setUserRole(profile?.role || "user");
|
| 69 |
+
setUserTeam(profile?.team_id || null);
|
| 70 |
+
}
|
| 71 |
+
} catch (err) {
|
| 72 |
+
console.error("[ClauseGuard Nav] Auth check error:", err);
|
| 73 |
+
if (!cancelled) {
|
| 74 |
+
setUserEmail(null);
|
| 75 |
+
setUserRole(null);
|
| 76 |
+
setUserTeam(null);
|
| 77 |
+
}
|
| 78 |
}
|
| 79 |
+
if (!cancelled) setLoaded(true);
|
|
|
|
| 80 |
}
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
// Check auth immediately
|
| 83 |
+
checkAuth();
|
| 84 |
+
|
| 85 |
+
// Listen for auth changes (login, logout, token refresh)
|
| 86 |
+
const { data: { subscription } } = supabase.auth.onAuthStateChange(
|
| 87 |
+
(event, session) => {
|
| 88 |
+
console.log("[ClauseGuard Nav] Auth event:", event);
|
| 89 |
+
|
| 90 |
+
if (event === "SIGNED_OUT" || !session?.user) {
|
| 91 |
+
setUserEmail(null);
|
| 92 |
+
setUserRole(null);
|
| 93 |
+
setUserTeam(null);
|
| 94 |
+
setLoaded(true);
|
| 95 |
+
return;
|
|
|
|
|
|
|
| 96 |
}
|
| 97 |
+
|
| 98 |
+
if (event === "SIGNED_IN" || event === "TOKEN_REFRESHED" || event === "INITIAL_SESSION") {
|
| 99 |
+
// We have a session — update email immediately, then fetch profile
|
| 100 |
+
setUserEmail(session.user.email || null);
|
| 101 |
+
setLoaded(true); // Show logged-in state immediately (even before profile loads)
|
| 102 |
+
|
| 103 |
+
// Fetch profile in background
|
| 104 |
+
supabase
|
| 105 |
+
.from("profiles")
|
| 106 |
+
.select("role, team_id")
|
| 107 |
+
.eq("id", session.user.id)
|
| 108 |
+
.single()
|
| 109 |
+
.then(({ data: profile, error }) => {
|
| 110 |
+
if (cancelled) return;
|
| 111 |
+
if (error) {
|
| 112 |
+
console.error("[ClauseGuard Nav] Profile error:", error.message);
|
| 113 |
+
setUserRole("user");
|
| 114 |
+
} else {
|
| 115 |
+
setUserRole(profile?.role || "user");
|
| 116 |
+
setUserTeam(profile?.team_id || null);
|
| 117 |
+
}
|
| 118 |
+
});
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
);
|
| 122 |
|
| 123 |
return () => {
|
| 124 |
+
cancelled = true;
|
| 125 |
+
subscription.unsubscribe();
|
| 126 |
};
|
| 127 |
+
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
async function handleSignOut() {
|
| 130 |
try {
|