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:
pradeepkumar
2026-03-16 13:25:32 +05:30
parent 63ad223c4c
commit 1e9262edfa
4 changed files with 26 additions and 9 deletions

View File

@@ -39,9 +39,14 @@ function TokenSync() {
hasInitialized.current = true;
}
} else if (status === "unauthenticated") {
// Clear tokens when logged out
localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken");
// Only clear tokens if a logout is explicitly in progress.
// Don't clear during normal page loads or brief unauthenticated flickers,
// as the login page saves tokens to localStorage before signIn completes.
const loggingOut = localStorage.getItem("isLoggingOut") === "true";
if (loggingOut) {
localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken");
}
hasInitialized.current = false;
}
}, [session, status]);