Skip to content

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

chore: add signed out redirect timeout (#35546)

Alaister Young08a7b75f1eaf
diff --git a/apps/studio/components/interfaces/SignIn/SessionTimeoutModal.tsx b/apps/studio/components/interfaces/SignIn/SessionTimeoutModal.tsx
new file mode 100644
index 00000000000..82ed566f169
--- /dev/null
+++ b/apps/studio/components/interfaces/SignIn/SessionTimeoutModal.tsx
@@ -0,0 +1,47 @@
+import * as Sentry from '@sentry/nextjs'
+import { useEffect } from 'react'
+
+import { InlineLink } from 'components/ui/InlineLink'
+import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
+
+interface SessionTimeoutModalProps {
+ visible: boolean
+ onClose: () => void
+ redirectToSignIn: () => void
+}
+
+export const SessionTimeoutModal = ({
+ visible,
+ onClose,
+ redirectToSignIn,
+}: SessionTimeoutModalProps) => {
+ useEffect(() => {
+ if (visible) {
+ Sentry.captureException(new Error('Session error detected'))
+ }
+ }, [visible])
+
+ return (
+ <ConfirmationModal
+ visible={visible}
+ title="Session timed out"
+ confirmLabel="Sign in again"
+ onCancel={onClose}
+ onConfirm={redirectToSignIn}
+ alert={{
+ base: { variant: 'warning' },
+ title: 'Your session has timed out',
+ description:
+ 'Please try signing in again. If you are not able to sign in again, please contact Support.',
+ }}
+ >
+ <p className="text-sm text-foreground-light">
+ Consider{' '}
+ <InlineLink href="https://github.com/orgs/supabase/discussions/36540">
+ generating a HAR file
+ </InlineLink>{' '}
+ from your session to help Support pinpoint the issue.
+ </p>
+ </ConfirmationModal>
+ )
+}
diff --git a/apps/studio/components/interfaces/UserDropdown.tsx b/apps/studio/components/interfaces/UserDropdown.tsx
index 2d215a0e0bc..d3efda8c593 100644
--- a/apps/studio/components/interfaces/UserDropdown.tsx
+++ b/apps/studio/components/interfaces/UserDropdown.tsx
@@ -123,7 +123,6 @@ export function UserDropdown() {
<DropdownMenuItem
onSelect={async () => {
await signOut()
- await router.push('/sign-in')
}}
>
Log out
diff --git a/apps/studio/hooks/misc/withAuth.tsx b/apps/studio/hooks/misc/withAuth.tsx
index e376b13d981..a7795ed2a8d 100644
--- a/apps/studio/hooks/misc/withAuth.tsx
+++ b/apps/studio/hooks/misc/withAuth.tsx
@@ -1,13 +1,16 @@
import { useRouter } from 'next/router'
-import { ComponentType, useEffect } from 'react'
+import { ComponentType, useCallback, useEffect, useRef, useState } from 'react'
import { toast } from 'sonner'
+import { SessionTimeoutModal } from 'components/interfaces/SignIn/SessionTimeoutModal'
import { usePermissionsQuery } from 'data/permissions/permissions-query'
import { useAuthenticatorAssuranceLevelQuery } from 'data/profile/mfa-authenticator-assurance-level-query'
-import { useAuth } from 'lib/auth'
-import { IS_PLATFORM } from 'lib/constants'
+import { useAuth, useSignOut } from 'lib/auth'
+import { BASE_PATH, IS_PLATFORM } from 'lib/constants'
import { NextPageWithLayout, isNextPageWithLayout } from 'types'
+const MAX_TIMEOUT = 10000 // 10 seconds
+
export function withAuth<T>(
WrappedComponent: ComponentType<T> | NextPageWithLayout<T, T>,
options: {
@@ -29,7 +32,12 @@ export function withAuth<T>(
const WithAuthHOC: ComponentType<T> = (props) => {
const router = useRouter()
+ const signOut = useSignOut()
const { isLoading, session } = useAuth()
+
+ const timeoutIdRef = useRef<NodeJS.Timeout | null>(null)
+ const [isSessionTimeoutModalOpen, setIsSessionTimeoutModalOpen] = useState(false)
+
const { isLoading: isAALLoading, data: aalData } = useAuthenticatorAssuranceLevelQuery({
onError(error) {
toast.error(
@@ -49,27 +57,74 @@ export function withAuth<T>(
const isLoggedIn = Boolean(session)
const isFinishedLoading = !isLoading && !isAALLoading
+ const redirectToSignIn = useCallback(() => {
+ let pathname = location.pathname
+ if (BASE_PATH) {
+ pathname = pathname.replace(BASE_PATH, '')
+ }
+
+ if (pathname === '/sign-in') {
+ // If the user is already on the sign in page, we don't need to redirect them
+ return
+ }
+
+ const searchParams = new URLSearchParams(location.search)
+ searchParams.set('returnTo', pathname)
+
+ // Sign out before redirecting to sign in page incase the user is stuck in a loading state
+ signOut().finally(() => {
+ router.push(`/sign-in?${searchParams.toString()}`)
+ })
+ }, [router, signOut])
+
useEffect(() => {
- const isCorrectLevel = options.useHighestAAL
- ? aalData?.currentLevel === aalData?.nextLevel
- : true
-
- if (isFinishedLoading && (!isLoggedIn || !isCorrectLevel)) {
- const searchParams = new URLSearchParams(location.search)
- let pathname = location.pathname
- if (process.env.NEXT_PUBLIC_BASE_PATH) {
- pathname = pathname.replace(process.env.NEXT_PUBLIC_BASE_PATH, '')
+ if (!isFinishedLoading) {
+ timeoutIdRef.current = setTimeout(() => {
+ setIsSessionTimeoutModalOpen(true)
+ }, MAX_TIMEOUT)
+ } else {
+ if (timeoutIdRef.current) {
+ clearTimeout(timeoutIdRef.current)
+ timeoutIdRef.current = null
}
+ }
- searchParams.set('returnTo', pathname)
+ return () => {
+ if (timeoutIdRef.current) {
+ clearTimeout(timeoutIdRef.current)
+ }
+ }
+ }, [isFinishedLoading, router, redirectToSignIn])
- router.push(`/sign-in?${searchParams.toString()}`)
+ const isCorrectLevel = options.useHighestAAL
+ ? aalData?.currentLevel === aalData?.nextLevel
+ : true
+
+ const shouldRedirect = isFinishedLoading && (!isLoggedIn || !isCorrectLevel)
+
+ useEffect(() => {
+ if (shouldRedirect) {
+ // Clear the timeout if it's still active and we are redirecting
+ if (timeoutIdRef.current) {
+ clearTimeout(timeoutIdRef.current)
+ timeoutIdRef.current = null
+ }
+ redirectToSignIn()
}
- }, [session, isLoading, router, aalData, isFinishedLoading, isLoggedIn])
+ }, [redirectToSignIn, shouldRedirect])
const InnerComponent = WrappedComponent as any
- return <InnerComponent {...props} />
+ return (
+ <>
+ <SessionTimeoutModal
+ visible={isSessionTimeoutModalOpen}
+ onClose={() => setIsSessionTimeoutModalOpen(false)}
+ redirectToSignIn={redirectToSignIn}
+ />
+ <InnerComponent {...props} />
+ </>
+ )
}
WithAuthHOC.displayName = `withAuth(${WrappedComponent.displayName})`
diff --git a/apps/studio/sentry.client.config.ts b/apps/studio/sentry.client.config.ts
index 933297b0de5..5e6b233d688 100644
--- a/apps/studio/sentry.client.config.ts
+++ b/apps/studio/sentry.client.config.ts
@@ -38,7 +38,9 @@ function isThirdPartyError(frames: Sentry.StackFrame[] | undefined) {
}
return !frames.some((frame, index) => {
- frame.abs_path?.startsWith('app:///_next') && !isSentryFrame(frame, index)
+ // Check both abs_path and filename for paths starting with app:///_next.
+ const path = frame.abs_path || frame.filename
+ return path?.startsWith('app:///_next') && !isSentryFrame(frame, index)
})
}
@@ -61,6 +63,7 @@ Sentry.init({
const isInvalidUrlEvent = (hint.originalException as any)?.message?.includes(
`Failed to construct 'URL': Invalid URL`
)
+
if (isInvalidUrlEvent && Math.random() > 0.01) {
return null
}
@@ -70,6 +73,7 @@ Sentry.init({
}
const frames = event.exception?.values?.[0].stacktrace?.frames || []
+
if (isThirdPartyError(frames)) {
return null
}