Skip to content

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

Fix all redirects so that they're fully functional.

Ivan Vasilov834c5fc0d80b
diff --git a/studio/components/interfaces/SignIn/SignInForm.tsx b/studio/components/interfaces/SignIn/SignInForm.tsx
index c58d2351751..fcfb9e20a42 100644
--- a/studio/components/interfaces/SignIn/SignInForm.tsx
+++ b/studio/components/interfaces/SignIn/SignInForm.tsx
@@ -5,7 +5,7 @@ import { useQueryClient } from '@tanstack/react-query'
import { getMfaAuthenticatorAssuranceLevel } from 'data/profile/mfa-authenticator-assurance-level-query'
import { useStore } from 'hooks'
import { usePushNext } from 'hooks/misc/useAutoAuthRedirect'
-import { auth } from 'lib/gotrue'
+import { auth, buildPathWithParams } from 'lib/gotrue'
import { incrementSignInClicks } from 'lib/local-storage'
import Link from 'next/link'
import { useRouter } from 'next/router'
@@ -60,7 +60,8 @@ const SignInForm = () => {
category: 'success',
message: `You need to provide your second factor authentication.`,
})
- router.replace('/sign-in-mfa')
+ const url = buildPathWithParams('/sign-in-mfa')
+ router.replace(url)
return
}
}
diff --git a/studio/components/interfaces/SignIn/SignInMfaForm.tsx b/studio/components/interfaces/SignIn/SignInMfaForm.tsx
index 0b5d4d09a42..08bcdbd4b80 100644
--- a/studio/components/interfaces/SignIn/SignInMfaForm.tsx
+++ b/studio/components/interfaces/SignIn/SignInMfaForm.tsx
@@ -1,6 +1,7 @@
import { AuthError } from '@supabase/gotrue-js'
import { Factor } from '@supabase/supabase-js'
import { useQueryClient } from '@tanstack/react-query'
+import Loading from 'components/ui/Loading'
import { useMfaChallengeAndVerifyMutation } from 'data/profile/mfa-challenge-and-verify-mutation'
import { useMfaListFactorsQuery } from 'data/profile/mfa-list-factors-query'
import { useStore } from 'hooks'
@@ -65,6 +66,10 @@ const SignInMfaForm = () => {
}
}
+ if (!selectedFactor) {
+ return <Loading />
+ }
+
return (
<Form
validateOnBlur
diff --git a/studio/components/interfaces/SignIn/SignInSSOForm.tsx b/studio/components/interfaces/SignIn/SignInSSOForm.tsx
index 5da7eaefa02..e53a992b2cb 100644
--- a/studio/components/interfaces/SignIn/SignInSSOForm.tsx
+++ b/studio/components/interfaces/SignIn/SignInSSOForm.tsx
@@ -2,7 +2,7 @@ import HCaptcha from '@hcaptcha/react-hcaptcha'
import { useQueryClient } from '@tanstack/react-query'
import { useStore } from 'hooks'
import { BASE_PATH } from 'lib/constants'
-import { auth, getReturnToPath } from 'lib/gotrue'
+import { auth, buildPathWithParams } from 'lib/gotrue'
import { useRef, useState } from 'react'
import { Button, Form, Input } from 'ui'
import { object, string } from 'yup'
@@ -30,15 +30,20 @@ const SignInSSOForm = () => {
token = captchaResponse?.response ?? null
}
+ // redirects to /sign-in to check if the user has MFA setup (handled in SignInLayout.tsx)
+ const redirectTo = buildPathWithParams(
+ `${
+ process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
+ ? location.origin
+ : process.env.NEXT_PUBLIC_SITE_URL
+ }${BASE_PATH}/sign-in-mfa`
+ )
+
const { data, error } = await auth.signInWithSSO({
domain: email.split('@')[1],
options: {
captchaToken: token ?? undefined,
- redirectTo: `${
- process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
- ? location.origin
- : process.env.NEXT_PUBLIC_SITE_URL
- }${BASE_PATH}${getReturnToPath('/')}`,
+ redirectTo,
},
})
diff --git a/studio/components/interfaces/SignIn/SignInWithGitHub.tsx b/studio/components/interfaces/SignIn/SignInWithGitHub.tsx
index 38edc76b4df..4a5eb812cc1 100644
--- a/studio/components/interfaces/SignIn/SignInWithGitHub.tsx
+++ b/studio/components/interfaces/SignIn/SignInWithGitHub.tsx
@@ -1,6 +1,6 @@
import * as Sentry from '@sentry/nextjs'
import { BASE_PATH } from 'lib/constants'
-import { auth, getReturnToPath } from 'lib/gotrue'
+import { auth, buildPathWithParams } from 'lib/gotrue'
import { incrementSignInClicks } from 'lib/local-storage'
import { useState } from 'react'
import { Button, IconGitHub } from 'ui'
@@ -17,14 +17,19 @@ const SignInWithGitHub = () => {
Sentry.captureMessage('Sign in without previous sign out detected')
}
+ // redirects to /sign-in to check if the user has MFA setup (handled in SignInLayout.tsx)
+ const redirectTo = buildPathWithParams(
+ `${
+ process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
+ ? location.origin
+ : process.env.NEXT_PUBLIC_SITE_URL
+ }${BASE_PATH}/sign-in-mfa`
+ )
+
const { error } = await auth.signInWithOAuth({
provider: 'github',
options: {
- redirectTo: `${
- process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
- ? location.origin
- : process.env.NEXT_PUBLIC_SITE_URL
- }${BASE_PATH}${getReturnToPath('/')}`,
+ redirectTo,
},
})
if (error) throw error
diff --git a/studio/components/layouts/SignInLayout/SignInLayout.tsx b/studio/components/layouts/SignInLayout/SignInLayout.tsx
index 8bc879ef557..cea1bea1203 100644
--- a/studio/components/layouts/SignInLayout/SignInLayout.tsx
+++ b/studio/components/layouts/SignInLayout/SignInLayout.tsx
@@ -3,7 +3,7 @@ import { useTheme } from 'common'
import { useFlag } from 'hooks'
import { usePushNext } from 'hooks/misc/useAutoAuthRedirect'
import { BASE_PATH } from 'lib/constants'
-import { auth, getAccessToken } from 'lib/gotrue'
+import { auth, buildPathWithParams, getAccessToken } from 'lib/gotrue'
import { observer } from 'mobx-react-lite'
import Image from 'next/image'
import Link from 'next/link'
@@ -32,17 +32,8 @@ const SignInLayout = ({
const { isDarkMode } = useTheme()
const ongoingIncident = useFlag('ongoingIncident')
+ // This useEffect redirects the user to MFA if they're already halfway signed in
useEffect(() => {
- const searchParams = new URLSearchParams(location.search)
- const hasReturnTo = searchParams.has('returnTo')
- const hasNext = searchParams.has('next')
- const shouldRedirect = !hasReturnTo && !hasNext
-
- if (!shouldRedirect) {
- // If there's a returnTo or next, then this redirect will be handled by useAutoAuthRedirect() in _app.tsx
- return
- }
-
auth
.initialize()
.then(async ({ error }) => {
@@ -66,7 +57,8 @@ const SignInLayout = ({
return
}
if (data.currentLevel !== data.nextLevel) {
- router.replace('/sign-in-mfa')
+ const redirectTo = buildPathWithParams('/sign-in-mfa')
+ router.replace(redirectTo)
return
}
}
@@ -76,7 +68,7 @@ const SignInLayout = ({
}
})
.catch(() => {}) // catch all errors thrown by auth methods
- }, [pushNext, queryClient, router])
+ }, [])
const [quote, setQuote] = useState<{
text: string
diff --git a/studio/hooks/misc/useAutoAuthRedirect.tsx b/studio/hooks/misc/useAutoAuthRedirect.tsx
index ca97fe8d486..6921bb3d6a3 100644
--- a/studio/hooks/misc/useAutoAuthRedirect.tsx
+++ b/studio/hooks/misc/useAutoAuthRedirect.tsx
@@ -1,5 +1,5 @@
import { QueryClient } from '@tanstack/react-query'
-import { auth, getReturnToPath } from 'lib/gotrue'
+import { auth, getAccessToken, getReturnToPath } from 'lib/gotrue'
import { useRouter } from 'next/router'
import { useCallback, useEffect } from 'react'
@@ -59,24 +59,33 @@ function useAutoAuthRedirect(queryClient: QueryClient) {
return
}
- ;(async () => {
- const { error } = await auth.initialize()
+ auth
+ .initialize()
+ .then(async ({ error }) => {
+ if (error) {
+ // if there was a problem signing in via the url, don't redirect
+ return
+ }
- if (error) {
- // if there was a problem signing in via the url, don't redirect
- return
- }
+ const token = await getAccessToken()
- const {
- data: { session },
- } = await auth.getSession()
+ if (token) {
+ const { data, error } = await auth.mfa.getAuthenticatorAssuranceLevel()
+ if (error) {
+ // if there was a problem fetching the user AAL levels, don't redirect
+ return
+ }
- if (session) {
- await queryClient.resetQueries()
+ // if the user doesn't have the appropriate AAL level, don't redirect
+ if (data.currentLevel !== data.nextLevel) {
+ return
+ }
- await pushNext()
- }
- })()
+ await queryClient.resetQueries()
+ await pushNext()
+ }
+ })
+ .catch(() => {}) // catch all errors thrown by auth methods
}, [])
}
diff --git a/studio/hooks/misc/withAuth.tsx b/studio/hooks/misc/withAuth.tsx
index 4ba0a37276b..e4168d8914a 100644
--- a/studio/hooks/misc/withAuth.tsx
+++ b/studio/hooks/misc/withAuth.tsx
@@ -63,7 +63,10 @@ export function withAuth<T>(WrappedComponent: ComponentType<T> | NextPageWithLay
useEffect(() => {
// This should run after setting store data
if (isRedirecting) {
- router.push(redirectTo)
+ const searchParams = new URLSearchParams(location.search)
+ searchParams.set('returnTo', location.pathname)
+ const url = `${redirectTo}?${searchParams.toString()}`
+ router.push(url)
}
}, [isRedirecting, redirectTo])
diff --git a/studio/lib/gotrue.ts b/studio/lib/gotrue.ts
index 0b7e7f7a41d..ad02f5c857f 100644
--- a/studio/lib/gotrue.ts
+++ b/studio/lib/gotrue.ts
@@ -1,9 +1,6 @@
-import { IS_PLATFORM } from './constants'
-import { User, Session } from '@supabase/gotrue-js'
+import { Session, User } from '@supabase/gotrue-js'
import { gotrueClient } from 'common'
-import { getFlags } from './configcat'
-
export { STORAGE_KEY } from 'common'
export const auth = gotrueClient
@@ -70,6 +67,14 @@ export const getIdentity = (gotrueUser: User) => {
}
}
+/**
+ * Transfers the search params from the current location path to a newly built path
+ */
+export const buildPathWithParams = (pathname: string) => {
+ const searchParams = new URLSearchParams(location.search)
+ return `${pathname}?${searchParams.toString()}`
+}
+
// NOTE: do not use any imports in this function as it is used standalone in the documents head
// [Joshen] Potentially can remove after full move over to /dashboard
export const getReturnToPath = (fallback = '/projects') => {