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();
|
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 () => {
|
const checkAuth = async () => {
|
||||||
try {
|
try {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
const hasSession = document.cookie.includes('accessToken');
|
|
||||||
|
|
||||||
if (!hasSession) {
|
|
||||||
console.log('No session → skipping auth check');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await authApi.getProfile();
|
const response = await authApi.getProfile();
|
||||||
|
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
const userData = response.data.user;
|
const { user: userData, organizations: orgs } = normalizeProfilePayload(response.data);
|
||||||
const orgs = response.data.organizations || [];
|
|
||||||
|
|
||||||
setUser(userData);
|
setUser(userData);
|
||||||
setOrganizations(orgs);
|
setOrganizations(orgs);
|
||||||
@@ -65,9 +78,12 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
if (storedOrgId && orgs.length > 0) {
|
if (storedOrgId && orgs.length > 0) {
|
||||||
const org = orgs.find(o => o.id === storedOrgId);
|
const org = orgs.find(o => o.id === storedOrgId);
|
||||||
if (org) setCurrentOrganization(org);
|
if (org) setCurrentOrganization(org);
|
||||||
} else if (orgs.length === 1) {
|
else setCurrentOrganization(null);
|
||||||
|
} else if (orgs.length === 1 && userData) {
|
||||||
setCurrentOrganization(orgs[0]);
|
setCurrentOrganization(orgs[0]);
|
||||||
localStorage.setItem('currentOrganizationId', orgs[0].id);
|
localStorage.setItem('currentOrganizationId', orgs[0].id);
|
||||||
|
} else {
|
||||||
|
setCurrentOrganization(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ export function middleware(request: NextRequest) {
|
|||||||
const token = request.cookies.get('accessToken')?.value;
|
const token = request.cookies.get('accessToken')?.value;
|
||||||
const isAuthenticated = !!token;
|
const isAuthenticated = !!token;
|
||||||
|
|
||||||
|
// If a logged-in user opens home, send them to dashboard.
|
||||||
|
if (isAuthenticated && pathname === '/') {
|
||||||
|
return NextResponse.redirect(new URL('/today', request.url));
|
||||||
|
}
|
||||||
|
|
||||||
// Always allow public routes first
|
// Always allow public routes first
|
||||||
if (publicRoutes.includes(pathname)) {
|
if (publicRoutes.includes(pathname)) {
|
||||||
// If user is already logged in and tries to access login/register → redirect to dashboard
|
// If user is already logged in and tries to access login/register → redirect to dashboard
|
||||||
|
|||||||
Reference in New Issue
Block a user