diff --git a/app/api/auth/options.ts b/app/api/auth/options.ts index ee6f390b..e181aa11 100644 --- a/app/api/auth/options.ts +++ b/app/api/auth/options.ts @@ -281,7 +281,7 @@ export const authOptions: NextAuthOptions = { return refreshedToken; }, async session({ session, token }) { - // If session was invalidated or tokens are missing, throw error to trigger sign out + // If session was invalidated or tokens are missing, return null to sign out if (token.error === "SessionNotActive" || token.error === "NoRefreshToken" || !token.accessToken || @@ -292,9 +292,11 @@ export const authOptions: NextAuthOptions = { hasRefreshToken: !!token.refreshToken }); - // Throw error to make NextAuth treat user as unauthenticated + // Return null to make NextAuth treat user as unauthenticated // This will trigger automatic redirect to sign-in page - throw new Error(token.error || "SessionInvalidated"); + // The client-side code will detect session invalidation by checking for + // session cookie existence when status is unauthenticated + return null as any; } // For other errors, throw to trigger error handling