diff --git a/studio/components/interfaces/SignIn/SignInForm.tsx b/studio/components/interfaces/SignIn/SignInForm.tsx index b65f297bcaf..ea8a82a4e55 100644 --- a/studio/components/interfaces/SignIn/SignInForm.tsx +++ b/studio/components/interfaces/SignIn/SignInForm.tsx @@ -1,8 +1,8 @@ import HCaptcha from '@hcaptcha/react-hcaptcha' import { useStore } from 'hooks' -import { auth, getNextPath } from 'lib/gotrue' +import { usePushNext } from 'hooks/misc/useAutoAuthRedirect' +import { auth } from 'lib/gotrue' import Link from 'next/link' -import { useRouter } from 'next/router' import { useRef, useState } from 'react' import { useSWRConfig } from 'swr' import { Button, Form, Input } from 'ui' @@ -15,7 +15,7 @@ const signInSchema = object({ const SignInForm = () => { const { ui } = useStore() - const router = useRouter() + const pushNext = usePushNext() const { cache } = useSWRConfig() const [captchaToken, setCaptchaToken] = useState(null) @@ -50,7 +50,7 @@ const SignInForm = () => { // @ts-ignore cache.clear() - await router.push(getNextPath()) + await pushNext() } else { setCaptchaToken(null) captchaRef.current?.resetCaptcha() diff --git a/studio/components/interfaces/SignIn/SignInWithGitHub.tsx b/studio/components/interfaces/SignIn/SignInWithGitHub.tsx index 96d21e5cfd4..f4e1f1549b6 100644 --- a/studio/components/interfaces/SignIn/SignInWithGitHub.tsx +++ b/studio/components/interfaces/SignIn/SignInWithGitHub.tsx @@ -1,4 +1,4 @@ -import { auth, getNextPath } from 'lib/gotrue' +import { auth, getReturnToPath } from 'lib/gotrue' import { Button, IconGitHub } from 'ui' const SignInWithGitHub = () => { @@ -11,7 +11,7 @@ const SignInWithGitHub = () => { process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview' ? process.env.NEXT_PUBLIC_VERCEL_URL : process.env.NEXT_PUBLIC_SITE_URL - }${getNextPath()}`, + }${getReturnToPath()}`, }, }) if (error) throw error diff --git a/studio/components/layouts/SignInLayout/SignInLayout.tsx b/studio/components/layouts/SignInLayout/SignInLayout.tsx index 31778e90284..51c50ec6287 100644 --- a/studio/components/layouts/SignInLayout/SignInLayout.tsx +++ b/studio/components/layouts/SignInLayout/SignInLayout.tsx @@ -1,11 +1,13 @@ import { useStore } from 'hooks' -import { getNextPath, STORAGE_KEY } from 'lib/gotrue' +import { usePushNext } from 'hooks/misc/useAutoAuthRedirect' +import { auth, getReturnToPath, 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 { PropsWithChildren, useEffect, useState } from 'react' import { tweets } from 'shared-data' +import { useSWRConfig } from 'swr' import { Button, IconFileText } from 'ui' type SignInLayoutProps = { @@ -22,9 +24,44 @@ const SignInLayout = ({ logoLinkToMarketingSite = false, children, }: PropsWithChildren) => { + const pushNext = usePushNext() const { ui } = useStore() + const { cache } = useSWRConfig() const { theme } = ui + 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 + } + + ;(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 pushNext() + } + })() + }, []) + const [quote, setQuote] = useState<{ text: string url: string @@ -43,7 +80,7 @@ const SignInLayout = ({