Skip to content

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

fix: add auto auth redirect back

Alaister Young30b77be26806
diff --git a/studio/components/interfaces/SignIn/SignInForm.tsx b/studio/components/interfaces/SignIn/SignInForm.tsx
index 6498075f943..c5cd86ec3a2 100644
--- a/studio/components/interfaces/SignIn/SignInForm.tsx
+++ b/studio/components/interfaces/SignIn/SignInForm.tsx
@@ -1,7 +1,7 @@
import HCaptcha from '@hcaptcha/react-hcaptcha'
import { useStore } from 'hooks'
import { useParams } from 'hooks/misc/useParams'
-import { auth } from 'lib/gotrue'
+import { auth, getNextPath } from 'lib/gotrue'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useRef, useState } from 'react'
@@ -17,7 +17,7 @@ const signInSchema = object({
const SignInForm = () => {
const { ui } = useStore()
const router = useRouter()
- const { returnTo } = useParams()
+ const { next: returnTo } = useParams()
const { cache } = useSWRConfig()
const [captchaToken, setCaptchaToken] = useState<string | null>(null)
@@ -52,7 +52,7 @@ const SignInForm = () => {
// @ts-ignore
cache.clear()
- await router.push(returnTo ?? '/projects')
+ await router.push(getNextPath(returnTo))
} else {
setCaptchaToken(null)
captchaRef.current?.resetCaptcha()
diff --git a/studio/components/interfaces/SignIn/SignInWithGitHub.tsx b/studio/components/interfaces/SignIn/SignInWithGitHub.tsx
index bd8ee8f0493..39c952ecf84 100644
--- a/studio/components/interfaces/SignIn/SignInWithGitHub.tsx
+++ b/studio/components/interfaces/SignIn/SignInWithGitHub.tsx
@@ -1,7 +1,10 @@
-import { auth } from 'lib/gotrue'
+import { useParams } from 'hooks'
+import { auth, getNextPath } from 'lib/gotrue'
import { Button, IconGitHub } from 'ui'
const SignInWithGitHub = () => {
+ const { next: returnTo } = useParams()
+
async function handleGithubSignIn() {
try {
const { error } = await auth.signInWithOAuth({
@@ -11,7 +14,7 @@ const SignInWithGitHub = () => {
process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
? process.env.NEXT_PUBLIC_VERCEL_URL
: process.env.NEXT_PUBLIC_SITE_URL
- }/projects`,
+ }${getNextPath(returnTo)}`,
},
})
if (error) throw error
diff --git a/studio/components/layouts/SignInLayout/SignInLayout.tsx b/studio/components/layouts/SignInLayout/SignInLayout.tsx
index 865db074cb5..0505e061267 100644
--- a/studio/components/layouts/SignInLayout/SignInLayout.tsx
+++ b/studio/components/layouts/SignInLayout/SignInLayout.tsx
@@ -1,14 +1,12 @@
-import { useParams, useStore } from 'hooks'
-import { auth, STORAGE_KEY } from 'lib/gotrue'
+import { useStore } from 'hooks'
+import { STORAGE_KEY } from 'lib/gotrue'
import { observer } from 'mobx-react-lite'
import Head from 'next/head'
import Image from 'next/image'
import Link from 'next/link'
-import { useRouter } from 'next/router'
import { PropsWithChildren, useEffect, useState } from 'react'
import { tweets } from 'shared-data'
-import { useSWRConfig } from 'swr'
-import { Button, IconFile, IconFileText } from 'ui'
+import { Button, IconFileText } from 'ui'
type SignInLayoutProps = {
heading: string
@@ -24,44 +22,9 @@ const SignInLayout = ({
logoLinkToMarketingSite = false,
children,
}: PropsWithChildren<SignInLayoutProps>) => {
- const { returnTo } = useParams()
- const router = useRouter()
- const { cache } = useSWRConfig()
const { ui } = useStore()
const { theme } = ui
- useEffect(() => {
- ;(async () => {
- const { error } = await auth.initialize()
-
- if (error) {
- ui.setNotification({
- category: 'error',
- message: error.message,
- })
-
- return
- }
-
- const {
- data: { session },
- } = await auth.getSession()
-
- if (session) {
- ui.setNotification({
- category: 'success',
- message: `Signed in successfully!`,
- })
-
- // .clear() does actually exist on the cache object, but it's not in the types 🤦🏻
- // @ts-ignore
- cache.clear()
-
- await router.push(returnTo ?? '/projects')
- }
- })()
- }, [])
-
const [quote, setQuote] = useState<{
text: string
url: string
@@ -80,7 +43,15 @@ const SignInLayout = ({
<Head>
<script
dangerouslySetInnerHTML={{
- __html: `if (window.localStorage.getItem('${STORAGE_KEY}')) {window.location.replace('/projects')}`,
+ __html: `if (localStorage.getItem('${STORAGE_KEY}')) {
+ let returnTo = new URLSearchParams(location.search).get('next') ?? '/projects'
+
+ if (returnTo === 'new-project') {
+ returnTo = '/new/new-project'
+ }
+
+ location.replace(returnTo)
+ }`,
}}
/>
</Head>
diff --git a/studio/hooks/misc/useAutoAuthRedirect.tsx b/studio/hooks/misc/useAutoAuthRedirect.tsx
new file mode 100644
index 00000000000..c67d948fa41
--- /dev/null
+++ b/studio/hooks/misc/useAutoAuthRedirect.tsx
@@ -0,0 +1,41 @@
+import { auth, getNextPath } from 'lib/gotrue'
+import { useRouter } from 'next/router'
+import { useEffect } from 'react'
+import { useSWRConfig } from 'swr'
+
+function useAutoAuthRedirect() {
+ const router = useRouter()
+ const { cache } = useSWRConfig()
+
+ useEffect(() => {
+ const returnTo = new URLSearchParams(location.search).get('next')
+
+ if (!returnTo) {
+ // If there's no returnTo, then we don't need to do anything
+ return
+ }
+
+ ;(async () => {
+ const { error } = await auth.initialize()
+
+ if (error) {
+ // if there was a problem signing in via the url, don't redirect
+ return
+ }
+
+ const {
+ data: { session },
+ } = await auth.getSession()
+
+ if (session) {
+ // .clear() does actually exist on the cache object, but it's not in the types 🤦🏻
+ // @ts-ignore
+ cache.clear()
+
+ await router.push(getNextPath(returnTo))
+ }
+ })()
+ }, [])
+}
+
+export default useAutoAuthRedirect
diff --git a/studio/hooks/misc/withAuth.tsx b/studio/hooks/misc/withAuth.tsx
index c6c84dbeec7..15776ca1f05 100644
--- a/studio/hooks/misc/withAuth.tsx
+++ b/studio/hooks/misc/withAuth.tsx
@@ -86,7 +86,7 @@ export function withAuth<T>(
to the login page if they are guaranteed (no token at all) to not be logged in. */}
<script
dangerouslySetInnerHTML={{
- __html: `if (!window.localStorage.getItem('${STORAGE_KEY}') && !window.location.hash) {window.location.replace('/?returnTo=' + encodeURIComponent(window.location.pathname + window.location.search + window.location.hash))}`,
+ __html: `if (!localStorage.getItem('${STORAGE_KEY}') && !location.hash) {location.replace('/sign-in?next=' + (new URLSearchParams(location.search).get('next') || encodeURIComponent(location.pathname + location.search + location.hash)))}`,
}}
/>
</Head>
diff --git a/studio/lib/gotrue.ts b/studio/lib/gotrue.ts
index 47094e91186..905dcacbfa6 100644
--- a/studio/lib/gotrue.ts
+++ b/studio/lib/gotrue.ts
@@ -37,3 +37,13 @@ export const getIdentity = (gotrueUser: User) => {
return { identity: null, error: err }
}
}
+
+export const getNextPath = (path?: string | null) => {
+ let next = path ?? '/projects'
+
+ if (next === 'new-project') {
+ return '/new/new-project'
+ }
+
+ return next
+}
diff --git a/studio/pages/_app.tsx b/studio/pages/_app.tsx
index deefe6cd8ee..358300caca2 100644
--- a/studio/pages/_app.tsx
+++ b/studio/pages/_app.tsx
@@ -33,6 +33,7 @@ import { dart } from 'lib/constants/prism'
import { PortalToast, RouteValidationWrapper, AppBannerWrapper } from 'components/interfaces/App'
import PageTelemetry from 'components/ui/PageTelemetry'
import FlagProvider from 'components/ui/Flag/FlagProvider'
+import useAutoAuthRedirect from 'hooks/misc/useAutoAuthRedirect'
dayjs.extend(customParseFormat)
dayjs.extend(utc)
@@ -90,6 +91,8 @@ function MyApp({ Component, pageProps }: AppPropsWithLayout) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
+ useAutoAuthRedirect()
+
const getLayout = Component.getLayout ?? ((page) => page)
return (