fix(auth): prevent logout on transient API failures

This commit is contained in:
2026-07-13 19:29:04 +05:30
parent 68829dc72b
commit 830883c6b7
2 changed files with 87 additions and 42 deletions

View File

@@ -1,19 +1,20 @@
'use client';
import { Button } from '@/components/ui/button';
import { initializeAuthenticatedApp } from '@/services/initializer.service';
import { useAppStore } from '@/store/app.store';
import { useAuthStore } from '@/store/auth.store';
import type { ReactNode } from 'react';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
export default function AppInitializer({ children }: { children: ReactNode }) {
const accessToken = useAuthStore((state) => state.accessToken);
const logout = useAuthStore((state) => state.logout);
const user = useAppStore((state) => state.user);
const loadStatus = useAppStore((state) => state.loadStatus);
const setLoading = useAppStore((state) => state.setLoading);
const setLoaded = useAppStore((state) => state.setLoaded);
const setLoadError = useAppStore((state) => state.setLoadError);
const clearApp = useAppStore((state) => state.clear);
const [retryAttempt, setRetryAttempt] = useState(0);
useEffect(() => {
let isActive = true;
@@ -30,8 +31,6 @@ export default function AppInitializer({ children }: { children: ReactNode }) {
await initializeAuthenticatedApp();
} catch {
if (isActive) {
logout();
clearApp();
setLoadError();
}
}
@@ -42,15 +41,32 @@ export default function AppInitializer({ children }: { children: ReactNode }) {
return () => {
isActive = false;
};
}, [
accessToken,
clearApp,
logout,
setLoaded,
setLoadError,
setLoading,
user,
]);
}, [accessToken, retryAttempt, setLoaded, setLoadError, setLoading, user]);
if (accessToken && !user && loadStatus === 'error') {
return (
<main className="flex min-h-screen items-center justify-center bg-background px-6">
<div
className="flex max-w-md flex-col items-center gap-4 text-center"
role="alert"
>
<div className="space-y-2">
<h1 className="text-xl font-semibold">Unable to load workspace</h1>
<p className="text-sm text-muted-foreground">
We could not load your account details. Your session has been
kept, so you can safely try again.
</p>
</div>
<Button
type="button"
onClick={() => setRetryAttempt((value) => value + 1)}
>
Try again
</Button>
</div>
</main>
);
}
return children;
}