Files
supabase/apps/studio/lib/auth.tsx
Pamela Chia 06fc7c4f4e fix(studio): clear telemetry cookies on sign-out (#53915)
Studio's sign-out never called the API to clear the HttpOnly telemetry
cookies (`user_id`, `organization_slug`, `project_ref`). Events sent
from the sign-in page after a sign-out were still recorded under the
user who had just signed out, and the next user on that browser
inherited the previous user's org and project cookies. I call the
existing `resetServerTelemetryCookies` helper (already used when consent
is withdrawn) from `useSignOut`, right after the auth sign-out, and only
when a session existed. A stored session whose refresh fails during
sign-out counts as existing.

**Note:** `withAuth` also calls `useSignOut` when a visitor with no
session opens a protected page. I skip the reset there because the
endpoint also clears the anonymous id and session id cookies, and a
visitor who has not signed in yet needs them until sign-in links them to
the account. When the reset does run, I make sign-out wait for it
(capped at 1 second by the helper) so the sign-in page cannot send an
event with the old cookies first.

## To test

Tested on the PR preview:
- [x] Sign in, open the network tab, then click "Sign out" in the avatar
menu: expect one `POST /platform/telemetry/reset` with a 2xx response.
Observed: one reset request (201), sent right after the auth `logout`
request (204).
- [x] While signed out, open a protected page such as
`/dashboard/organizations`: expect a redirect to sign-in and no `POST
/platform/telemetry/reset`. Observed: redirect to
`/dashboard/sign-in?returnTo=%2Forganizations`, no reset request.

The preview is cross-site to the staging API, so the browser never
stores these `SameSite=Lax` cookies there and the cookie-clearing effect
cannot be observed on it. I'll check that on the staging dashboard after
merge.

## Linear
- fixes GROWTH-1556
2026-11-24 18:12:36 +08:00

64 lines
1.9 KiB
TypeScript

import { useQueryClient } from '@tanstack/react-query'
import {
AuthProvider as AuthProviderInternal,
clearLocalStorage,
gotrueClient,
posthogClient,
resetServerTelemetryCookies,
useAuthError,
} from 'common'
import { PropsWithChildren, useCallback, useEffect } from 'react'
import { toast } from 'sonner'
import { GOTRUE_ERRORS, IS_PLATFORM } from './constants'
import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
const AuthErrorToaster = ({ children }: PropsWithChildren) => {
const error = useAuthError()
useEffect(() => {
if (error !== null) {
// Check for unverified GitHub users after a GitHub sign in
if (error.message === GOTRUE_ERRORS.UNVERIFIED_GITHUB_USER) {
toast.error(
'Please verify your email on GitHub first, then reach out to us at support@supabase.io to log into the dashboard'
)
return
}
toast.error(error.message)
}
}, [error])
return children
}
export const AuthProvider = ({ children }: PropsWithChildren) => {
return (
<AuthProviderInternal alwaysLoggedIn={!IS_PLATFORM}>
<AuthErrorToaster>{children}</AuthErrorToaster>
</AuthProviderInternal>
)
}
export function useSignOut() {
const queryClient = useQueryClient()
const { clearStorage: clearAssistantStorage } = useAiAssistantStateSnapshot()
return useCallback(async () => {
const { data, error } = await gotrueClient.getSession()
const wasSignedIn = data.session !== null || error !== null
const result = await gotrueClient.signOut()
// withAuth also calls this for visitors with no session, whose anonymous id cookie must
// survive until sign-in links it to their account
if (wasSignedIn) await resetServerTelemetryCookies()
posthogClient.reset()
clearLocalStorage()
// Clear Assistant IndexedDB
await clearAssistantStorage()
queryClient.clear()
return result
}, [queryClient, clearAssistantStorage])
}