fix: Prevent token race conditions by pre-saving access/refresh tokens to localStorage before NextAuth signIn and refining logout token clearing.
This commit is contained in:
@@ -56,6 +56,13 @@ export default function LoginPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save tokens to localStorage BEFORE signIn to prevent race condition
|
||||
// (dashboard components may call API before TokenSync has synced from session)
|
||||
if (validateData.data?.accessToken) {
|
||||
localStorage.setItem('accessToken', validateData.data.accessToken);
|
||||
localStorage.setItem('refreshToken', validateData.data.refreshToken);
|
||||
}
|
||||
|
||||
// If validation passed, use NextAuth to create session
|
||||
const result = await signIn('credentials', {
|
||||
email,
|
||||
@@ -66,6 +73,9 @@ export default function LoginPage() {
|
||||
setLoading(false);
|
||||
|
||||
if (result?.error) {
|
||||
// Login failed - clear the tokens we just saved
|
||||
localStorage.removeItem('accessToken');
|
||||
localStorage.removeItem('refreshToken');
|
||||
setError('Login failed. Please try again.');
|
||||
} else {
|
||||
router.push('/');
|
||||
|
||||
Reference in New Issue
Block a user