@@ -314,13 +314,18 @@ const RedirectGuard = () => {
314314 return ;
315315 }
316316
317+ // Explicitly check the loading state to make the guard more robust.
318+ // Even with a stabilized `isAuthenticatedAtom`, this prevents any possibility of
319+ // redirecting during a transient loading state (an "auth flap").
320+ const isAuthLoading = authLoadableValue . state === 'loading' ;
321+
317322 // Get `canRefresh` from the latest auth data, even if loading.
318323 const canRefresh = authLoadableValue . state === 'hasData' && authLoadableValue . data . expired_access_token_call_refresh ;
319324 const publicPaths = [ '/login' ] ;
320325
321- // Use the stabilized `isAuthenticated` atom to prevent redirects during auth flaps.
322- // Redirect if not authenticated, not on a public path, and a token refresh isn't pending.
323- if ( ! isAuthenticated && ! canRefresh && ! publicPaths . some ( p => pathname . startsWith ( p ) ) ) {
326+ // Redirect if auth is not loading, user is not authenticated, not on a public path,
327+ // and a token refresh isn't pending.
328+ if ( ! isAuthLoading && ! isAuthenticated && ! canRefresh && ! publicPaths . some ( p => pathname . startsWith ( p ) ) ) {
324329 // The path has already been saved by PathSaver. Just trigger the redirect.
325330 setPendingRedirect ( '/login' ) ;
326331 }
@@ -662,6 +667,8 @@ export const StateInspector = () => {
662667
663668 // Atoms for redirect logic debugging
664669 const pathname = usePathname ( ) ; // Get current pathname
670+ const isAuthenticatedValue = useAtomValue ( isAuthenticatedAtom ) ;
671+ const initialAuthCheckCompletedValue = useAtomValue ( initialAuthCheckCompletedAtom ) ;
665672 const pendingRedirectValue = useAtomValue ( pendingRedirectAtom ) ;
666673 const requiredSetupRedirectValue = useAtomValue ( requiredSetupRedirectAtom ) ;
667674 const loginActionInProgressValue = useAtomValue ( loginActionInProgressAtom ) ;
@@ -738,6 +745,8 @@ export const StateInspector = () => {
738745 lastKnownPathBeforeAuthChange : lastKnownPathValue ,
739746 } ,
740747 redirectRelevantState : {
748+ isAuthenticated_STABLE : isAuthenticatedValue ,
749+ initialAuthCheckCompleted : initialAuthCheckCompletedValue ,
741750 authCheckDone : authLoadableValue . state !== 'loading' ,
742751 isRestClientReady : ! ! restClientFromAtom ,
743752 activityStandard : activityStandardFromAtom , // This is the actual data or null
@@ -844,6 +853,9 @@ export const StateInspector = () => {
844853 < div >
845854 < strong > Navigation & Redirect Debugging :</ strong >
846855 < div className = "pl-4 mt-1 space-y-1" >
856+ < div > < strong > (Stable) isAuthenticated:</ strong > { isAuthenticatedValue ? 'Yes' : 'No' } </ div >
857+ < div > < strong > Initial Auth Check Completed:</ strong > { initialAuthCheckCompletedValue ? 'Yes' : 'No' } </ div >
858+ < hr className = "my-1 border-gray-500" />
847859 < div > < strong > Pathname:</ strong > { pathname } </ div >
848860 < div > < strong > Active Redirect Target:</ strong > { ( pendingRedirectValue || requiredSetupRedirectValue ) || 'None' } </ div >
849861 < div > < strong > Pending Redirect:</ strong > { pendingRedirectValue || 'None' } </ div >
0 commit comments