bugfix: authenticated users seen a blank page instead of their dashboard when reopen the site
This commit is contained in:
@@ -41,22 +41,35 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
const normalizeProfilePayload = (payload: any): { user: User | null; organizations: Organization[] } => {
|
||||
const organizations = payload?.organizations || [];
|
||||
|
||||
if (payload?.user) {
|
||||
return { user: payload.user as User, organizations };
|
||||
}
|
||||
|
||||
if (payload?.id && payload?.email && payload?.name) {
|
||||
return {
|
||||
user: {
|
||||
id: payload.id,
|
||||
email: payload.email,
|
||||
name: payload.name,
|
||||
},
|
||||
organizations,
|
||||
};
|
||||
}
|
||||
|
||||
return { user: null, organizations };
|
||||
};
|
||||
|
||||
const checkAuth = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
const hasSession = document.cookie.includes('accessToken');
|
||||
|
||||
if (!hasSession) {
|
||||
console.log('No session → skipping auth check');
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await authApi.getProfile();
|
||||
|
||||
if (response.success) {
|
||||
const userData = response.data.user;
|
||||
const orgs = response.data.organizations || [];
|
||||
const { user: userData, organizations: orgs } = normalizeProfilePayload(response.data);
|
||||
|
||||
setUser(userData);
|
||||
setOrganizations(orgs);
|
||||
@@ -65,9 +78,12 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
if (storedOrgId && orgs.length > 0) {
|
||||
const org = orgs.find(o => o.id === storedOrgId);
|
||||
if (org) setCurrentOrganization(org);
|
||||
} else if (orgs.length === 1) {
|
||||
else setCurrentOrganization(null);
|
||||
} else if (orgs.length === 1 && userData) {
|
||||
setCurrentOrganization(orgs[0]);
|
||||
localStorage.setItem('currentOrganizationId', orgs[0].id);
|
||||
} else {
|
||||
setCurrentOrganization(null);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user