Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(studio): preserve session and redirect to MFA when AAL elevation is needed (#47145)

Charis0abfbdd3d7c1
diff --git a/apps/studio/hooks/misc/withAuth.tsx b/apps/studio/hooks/misc/withAuth.tsx
index 0088f9f9a08..b480149fbf9 100644
--- a/apps/studio/hooks/misc/withAuth.tsx
+++ b/apps/studio/hooks/misc/withAuth.tsx
@@ -67,13 +67,18 @@ export function withAuth<T>(
const isLoggedIn = Boolean(session)
const isFinishedLoading = !isLoading && !isAALLoading
+ const isCorrectLevel = options.useHighestAAL
+ ? aalData?.currentLevel === aalData?.nextLevel
+ : true
+ const needsMfaElevation = isLoggedIn && !isCorrectLevel
+
const redirectToSignIn = useCallback(() => {
let pathname = location.pathname
if (BASE_PATH) {
pathname = pathname.replace(BASE_PATH, '')
}
- if (pathname === '/sign-in') {
+ if (pathname === '/sign-in' || pathname === '/sign-in-mfa') {
// If the user is already on the sign in page, we don't need to redirect them
return
}
@@ -81,11 +86,19 @@ export function withAuth<T>(
const searchParams = new URLSearchParams(location.search)
searchParams.set('returnTo', pathname)
+ if (needsMfaElevation) {
+ // Session is valid at AAL1 but needs to be elevated to AAL2. Preserve the session
+ // and send the user to the MFA challenge — typically the IdP-initiated SSO path,
+ // where the user lands directly on /dashboard without going through /sign-in-mfa.
+ router.push(`/sign-in-mfa?${searchParams.toString()}`)
+ return
+ }
+
// Sign out before redirecting to sign in page incase the user is stuck in a loading state
signOut().finally(() => {
router.push(`/sign-in?${searchParams.toString()}`)
})
- }, [router, signOut])
+ }, [router, signOut, needsMfaElevation])
useEffect(() => {
if (!isFinishedLoading) {
@@ -106,10 +119,6 @@ export function withAuth<T>(
}
}, [isFinishedLoading, router, redirectToSignIn])
- const isCorrectLevel = options.useHighestAAL
- ? aalData?.currentLevel === aalData?.nextLevel
- : true
-
const shouldRedirect = isFinishedLoading && (!isLoggedIn || !isCorrectLevel)
useEffect(() => {