Skip to content

Commit 9732be9

Browse files
authored
fix(passport): fix is authenticated state (#2786)
1 parent 1a3263d commit 9732be9

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

packages/auth-next-client/src/hooks.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)