Skip to content

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

fix infinite sign in redirect loop

Alaister Young1cc91a989ece
diff --git a/studio/components/interfaces/App/RouteValidationWrapper.tsx b/studio/components/interfaces/App/RouteValidationWrapper.tsx
index a0cac70e50f..f6ca98bf22d 100644
--- a/studio/components/interfaces/App/RouteValidationWrapper.tsx
+++ b/studio/components/interfaces/App/RouteValidationWrapper.tsx
@@ -47,7 +47,7 @@ const RouteValidationWrapper: FC = ({ children }) => {
if (!isValidOrg) {
ui.setNotification({ category: 'error', message: 'This organization does not exist' })
- router.push('/')
+ router.push('/projects')
return
}
}
@@ -64,7 +64,7 @@ const RouteValidationWrapper: FC = ({ children }) => {
if (!isValidProject) {
ui.setNotification({ category: 'error', message: 'This project does not exist' })
- router.push('/')
+ router.push('/projects')
return
}
}
diff --git a/studio/components/layouts/AccountLayout/AccountLayout.tsx b/studio/components/layouts/AccountLayout/AccountLayout.tsx
index 87ce2e998a8..fb2b08c1e5b 100644
--- a/studio/components/layouts/AccountLayout/AccountLayout.tsx
+++ b/studio/components/layouts/AccountLayout/AccountLayout.tsx
@@ -3,7 +3,7 @@ import { FC, ReactNode } from 'react'
import { useRouter } from 'next/router'
import { observer } from 'mobx-react-lite'
-import { auth } from 'lib/gotrue'
+import { auth, STORAGE_KEY } from 'lib/gotrue'
import { useStore, withAuth, useFlag } from 'hooks'
import { API_URL, IS_PLATFORM } from 'lib/constants'
import WithSidebar from './WithSidebar'
@@ -27,7 +27,7 @@ const AccountLayout: FC<Props> = ({ children, title, breadcrumbs }) => {
const onClickLogout = async () => {
await auth.signOut()
- localStorage.clear()
+ localStorage.removeItem(STORAGE_KEY)
await router.push('/sign-in')
router.reload()
}
diff --git a/studio/hooks/misc/withAuth.tsx b/studio/hooks/misc/withAuth.tsx
index 6fe163e07e0..c0f33c58669 100644
--- a/studio/hooks/misc/withAuth.tsx
+++ b/studio/hooks/misc/withAuth.tsx
@@ -5,6 +5,7 @@ import { NextRouter, useRouter } from 'next/router'
import { STORAGE_KEY } from 'lib/gotrue'
import { IS_PLATFORM } from 'lib/constants'
import { useProfile, useStore, usePermissions } from 'hooks'
+import Error500 from '../../pages/500'
const PLATFORM_ONLY_PAGES = ['storage', 'reports', 'settings']
@@ -28,7 +29,7 @@ export function withAuth<T>(
const returning =
app.projects.isInitialized && app.organizations.isInitialized ? 'minimal' : undefined
- const { profile, isLoading } = useProfile(returning)
+ const { profile, isLoading, error } = useProfile(returning)
const {
permissions,
isLoading: isPermissionLoading,
@@ -38,7 +39,7 @@ export function withAuth<T>(
const isAccessingBlockedPage = !IS_PLATFORM && PLATFORM_ONLY_PAGES.includes(page)
const isRedirecting =
isAccessingBlockedPage ||
- checkRedirectTo(isLoading, router, profile, redirectTo, redirectIfFound)
+ checkRedirectTo(isLoading, router, profile, error, redirectTo, redirectIfFound)
useEffect(() => {
// This should run before redirecting
@@ -73,6 +74,10 @@ export function withAuth<T>(
}
}, [isLoading, router.isReady, ref, slug])
+ if (!isLoading && !isRedirecting && !profile && error) {
+ return <Error500 />
+ }
+
return (
<>
<Head>
@@ -91,21 +96,23 @@ export function withAuth<T>(
}
function defaultRedirectTo(ref: string | string[] | undefined) {
- return IS_PLATFORM ? '/' : ref !== undefined ? `/project/${ref}` : '/'
+ return IS_PLATFORM ? '/sign-in' : ref !== undefined ? `/project/${ref}` : '/sign-in'
}
function checkRedirectTo(
loading: boolean,
router: NextRouter,
profile: any,
+ profileError: any,
redirectTo: string,
redirectIfFound?: boolean
) {
if (loading) return false
if (router.pathname == redirectTo) return false
- // If redirectTo is set, redirect if the user was not found.
- if (redirectTo && !redirectIfFound && !profile) return true
+ // If redirectTo is set, redirect if the user is not logged in.
+ if (redirectTo && !redirectIfFound && profileError?.code === 401) return true
+
// If redirectIfFound is also set, redirect if the user was found
if (redirectIfFound && profile) return true
diff --git a/studio/hooks/queries/useProfile.ts b/studio/hooks/queries/useProfile.ts
index 33a5aa29581..b4f8f78d79f 100644
--- a/studio/hooks/queries/useProfile.ts
+++ b/studio/hooks/queries/useProfile.ts
@@ -29,6 +29,7 @@ export function useProfile(returning?: 'minimal') {
profile: anyError ? undefined : data,
isLoading: !anyError && !data,
isError: !!anyError,
+ error: anyError,
mutateProfile,
}
}
diff --git a/studio/pages/500.tsx b/studio/pages/500.tsx
index a01bc3ee6c5..9c5ab86c18e 100644
--- a/studio/pages/500.tsx
+++ b/studio/pages/500.tsx
@@ -1,17 +1,27 @@
import { NextPage } from 'next'
+import { useRouter } from 'next/router'
import Link from 'next/link'
import Image from 'next/image'
import { Button } from 'ui'
import { observer } from 'mobx-react-lite'
import { useStore } from 'hooks'
+import { auth, STORAGE_KEY } from 'lib/gotrue'
-const Error500: NextPage = ({}) => {
+const Error500: NextPage = () => {
+ const router = useRouter()
const { ui } = useStore()
const { theme } = ui
+ const onClickLogout = async () => {
+ await auth.signOut()
+ localStorage.removeItem(STORAGE_KEY)
+ await router.push('/sign-in')
+ router.reload()
+ }
+
return (
- <div className="relative mx-auto flex h-full w-full flex-col items-center justify-center space-y-6">
+ <div className="relative mx-auto flex flex-1 w-full flex-col items-center justify-center space-y-6">
<div className="absolute top-0 mx-auto w-full max-w-7xl px-8 pt-6 sm:px-6 lg:px-8">
<nav className="relative flex items-center justify-between sm:h-10">
<div className="flex flex-shrink-0 flex-grow items-center lg:flex-grow-0">
@@ -36,11 +46,15 @@ const Error500: NextPage = ({}) => {
</p>
</div>
<div className="flex items-center space-x-4">
- <Link href="/projects">
- <a>
- <Button>Head back</Button>
- </a>
- </Link>
+ {router.pathname !== '/projects' ? (
+ <Link href="/projects">
+ <a>
+ <Button>Head back</Button>
+ </a>
+ </Link>
+ ) : (
+ <Button onClick={onClickLogout}>Head back</Button>
+ )}
<Link href="/support/new">
<a>
<Button type="secondary">Submit a support request</Button>