-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(auth): recover cleanly when an impersonation session expires #5692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...sim/app/workspace/[workspaceId]/components/impersonation-banner/impersonation-expired.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| 'use client' | ||
|
|
||
| import { useCallback, useEffect, useRef, useState } from 'react' | ||
| import { Chip } from '@sim/emcn' | ||
| import { useSession } from '@/lib/auth/auth-client' | ||
| import { recoverFromStaleSession } from '@/lib/auth/stale-session-recovery' | ||
|
|
||
| /** | ||
| * Cleanly logs out when an impersonation session expires while the app is | ||
| * mounted. | ||
| * | ||
| * Detection keys off the transition: this only activates when the session | ||
| * query settles to `null` after a session that carried `impersonatedBy` (the | ||
| * expired session is deleted server-side, so the live session can't be used). | ||
| * | ||
| * Recovery goes through {@link recoverFromStaleSession}: the expired session's | ||
| * cookies are never cleared server-side (better-auth's customSession plugin | ||
| * swallows the deletion headers), and the login route bounces any request | ||
| * still carrying a session cookie back to /workspace. The helper signs out | ||
| * (which clears the cookies without requiring a live session), wipes per-user | ||
| * client state, and only navigates when the sign-out succeeded — navigating | ||
| * after a failed sign-out would recreate the redirect loop. | ||
| */ | ||
| export function ImpersonationExpired() { | ||
| const { data: session, isPending, error } = useSession() | ||
| const startedRef = useRef(false) | ||
| const [sawImpersonation, setSawImpersonation] = useState(false) | ||
| const [failed, setFailed] = useState(false) | ||
|
|
||
| if (!sawImpersonation && session?.session?.impersonatedBy) { | ||
| setSawImpersonation(true) | ||
| } | ||
|
|
||
| const expired = sawImpersonation && !isPending && !error && !session?.user | ||
|
|
||
| const attemptRecovery = useCallback(() => { | ||
| setFailed(false) | ||
| void recoverFromStaleSession().then((recovered) => { | ||
| if (!recovered) setFailed(true) | ||
| }) | ||
| }, []) | ||
|
|
||
| useEffect(() => { | ||
| if (!expired || startedRef.current) return | ||
| startedRef.current = true | ||
| attemptRecovery() | ||
| }, [expired, attemptRecovery]) | ||
|
|
||
| if (!expired) { | ||
| return null | ||
| } | ||
|
|
||
| return ( | ||
| <main className='fixed inset-0 z-50 flex flex-col items-center justify-center gap-3 bg-[var(--surface-1)] p-6'> | ||
| <p className='text-[var(--text-muted)] text-sm'> | ||
| {failed | ||
| ? 'The impersonation session expired, but signing out failed.' | ||
| : 'The impersonation session expired. Signing you out…'} | ||
| </p> | ||
| {failed && ( | ||
| <Chip variant='primary' onClick={attemptRecovery}> | ||
| Try again | ||
| </Chip> | ||
| )} | ||
| </main> | ||
| ) | ||
| } | ||
1 change: 1 addition & 0 deletions
1
apps/sim/app/workspace/[workspaceId]/components/impersonation-banner/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { ImpersonationBanner } from './impersonation-banner' | ||
| export { ImpersonationExpired } from './impersonation-expired' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 'use client' | ||
|
|
||
| import { createLogger } from '@sim/logger' | ||
| import { signOut } from '@/lib/auth/auth-client' | ||
| import { clearUserData } from '@/stores' | ||
|
|
||
| const logger = createLogger('StaleSessionRecovery') | ||
|
|
||
| /** | ||
| * Signs out (clearing every auth cookie server-side), wipes per-user client | ||
| * state, and navigates to login. Returns false without navigating when the | ||
| * sign-out request fails — the cookies are still set, so going to /login | ||
| * would only get bounced back to /workspace by the middleware. | ||
| * | ||
| * Shared by the /workspace loader (stale-cookie 401s and clean-null sessions) | ||
| * and the impersonation-expired screen, so every identity-recovery path clears | ||
| * cookies and persisted client state the same way. | ||
| */ | ||
| export async function recoverFromStaleSession(): Promise<boolean> { | ||
| try { | ||
| await signOut() | ||
| } catch (error) { | ||
| logger.error('Failed to sign out while recovering from a stale session:', error) | ||
| return false | ||
| } | ||
| await clearUserData() | ||
| window.location.assign('/login') | ||
| return true | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.