Skip to content

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

Redirect the user to /sign-in page if he doesn't have the appropriate AA level.

Ivan Vasilov959b38479f0e
diff --git a/studio/hooks/misc/withAuth.tsx b/studio/hooks/misc/withAuth.tsx
index 21812c5c3f1..4ba0a37276b 100644
--- a/studio/hooks/misc/withAuth.tsx
+++ b/studio/hooks/misc/withAuth.tsx
@@ -4,11 +4,12 @@ import { ComponentType, useEffect } from 'react'
import { useParams } from 'common/hooks'
import { usePermissionsQuery } from 'data/permissions/permissions-query'
+import { useAuthenticatorAssuranceLevelQuery } from 'data/profile/mfa-authenticator-assurance-level-query'
import { useSelectedProject, useStore } from 'hooks'
import { useAuth } from 'lib/auth'
import { IS_PLATFORM } from 'lib/constants'
-import { STORAGE_KEY, getReturnToPath } from 'lib/gotrue'
-import { NextPageWithLayout, isNextPageWithLayout } from 'types'
+import { getReturnToPath, STORAGE_KEY } from 'lib/gotrue'
+import { isNextPageWithLayout, NextPageWithLayout } from 'types'
import Error500 from '../../pages/500'
const PLATFORM_ONLY_PAGES = [
@@ -19,26 +20,19 @@ const PLATFORM_ONLY_PAGES = [
'auth/url-configuration',
]
-export function withAuth<T>(
- WrappedComponent: ComponentType<T> | NextPageWithLayout<T, T>,
- options?: {
- redirectTo: string
- /* run the redirect if the user is logged in */
- redirectIfFound?: boolean
- }
-) {
+export function withAuth<T>(WrappedComponent: ComponentType<T> | NextPageWithLayout<T, T>) {
const WithAuthHOC: ComponentType<T> = (props: any) => {
const router = useRouter()
const { basePath } = router
const { ref } = useParams()
const rootStore = useStore()
const { isLoading, session } = useAuth()
+ const { isLoading: isAALLoading, data: aalData } = useAuthenticatorAssuranceLevelQuery()
const { ui } = rootStore
const page = router.pathname.split('/').slice(3).join('/')
- const redirectTo = options?.redirectTo ?? defaultRedirectTo(ref)
- const redirectIfFound = options?.redirectIfFound
+ const redirectTo = defaultRedirectTo(ref)
usePermissionsQuery({
onError(error: any) {
@@ -51,13 +45,20 @@ export function withAuth<T>(
})
const isLoggedIn = Boolean(session)
+ const isCorrectLevel = aalData?.currentLevel === aalData?.nextLevel
const isAccessingBlockedPage =
!IS_PLATFORM &&
PLATFORM_ONLY_PAGES.some((platformOnlyPage) => page.startsWith(platformOnlyPage))
const isRedirecting =
isAccessingBlockedPage ||
- checkRedirectTo(isLoading, router.pathname, isLoggedIn, redirectTo, redirectIfFound)
+ checkRedirectTo(
+ isLoading || isAALLoading,
+ router.pathname,
+ isLoggedIn,
+ isCorrectLevel,
+ redirectTo
+ )
useEffect(() => {
// This should run after setting store data
@@ -114,17 +115,14 @@ function checkRedirectTo(
loading: boolean,
pathname: string,
isLoggedIn: boolean,
- redirectTo: string,
- redirectIfFound?: boolean
+ isCorrectLevel: boolean,
+ redirectTo: string
) {
if (loading) return false
if (pathname === redirectTo) return false
- // If redirectTo is set, redirect if the user is not logged in.
- if (redirectTo && !redirectIfFound && !isLoggedIn) return true
-
- // If redirectIfFound is also set, redirect if the user was found
- if (redirectIfFound && isLoggedIn) return true
+ // If redirectTo is set, redirect if the user is not logged in or logged in with MFA.
+ if (redirectTo && (!isLoggedIn || !isCorrectLevel)) return true
return false
}