File tree Expand file tree Collapse file tree
packages/auth-next-client/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -97,15 +97,19 @@ export function useImmutableSession(): UseImmutableSessionReturn {
9797
9898 const isLoading = status === 'loading' ;
9999
100- // Core authentication check from NextAuth
101- const hasSession = status === 'authenticated' && ! ! session ;
102-
103- // During loading/refreshing, keep showing authenticated if we had a session (avoids UI flicker
100+ // Core authentication check - user has a valid session with usable access token.
101+ // A session can exist but be unusable if the access token is missing or refresh failed.
102+ const hasValidSession = status === 'authenticated'
103+ && ! ! session
104+ && ! ! session . accessToken
105+ && ! session . error ;
106+
107+ // During loading/refreshing, keep showing authenticated if we had a valid session (avoids UI flicker
104108 // when NextAuth refetches on window focus or after getUser(forceRefresh)).
105109 const hadSessionRef = useRef ( false ) ;
106- if ( hasSession ) hadSessionRef . current = true ;
107- if ( ! hasSession && ! isLoading && ! isRefreshing ) hadSessionRef . current = false ;
108- const isAuthenticated = hasSession || ( ( isLoading || isRefreshing ) && hadSessionRef . current ) ;
110+ if ( hasValidSession ) hadSessionRef . current = true ;
111+ if ( ! hasValidSession && ! isLoading && ! isRefreshing ) hadSessionRef . current = false ;
112+ const isAuthenticated = hasValidSession || ( ( isLoading || isRefreshing ) && hadSessionRef . current ) ;
109113
110114 // Use a ref to always have access to the latest session.
111115 // This avoids stale closure issues when the wallet stores the getUser function
You can’t perform that action at this time.
0 commit comments