Files
supabase/apps/studio/data/profile/profile-identities-query.ts
Danny White 04ea14c103 fix(studio): isolate account recovery checks by user (#52120)
## Problem

Account checks need consistent query gating and recovery-gap evaluation.
Sign-in and sign-out already reset the query cache; user-scoped keys add
defence in depth.

## Solution

Tie account query caches to the signed-in person, honour query gating,
and centralise recovery-gap evaluation. No layout changes.

## Review instructions

1. Open [Account
Security](https://studio-staging-git-dnywh-recovery-account-queries-supabase.vercel.app/dashboard/account/security)
in staging, or `/account/security` locally. Sign in with a test account
that has MFA enabled; confirm its authenticators and recovery-code
status load normally.
2. Sign out and sign in as a different test account with a different MFA
setup. Confirm the page shows that account’s setup, without the previous
account’s authenticators or code status.
3. Reload with MFA enabled and complete the sign-in challenge. Account
Security should load normally after verification.
2026-10-27 12:29:10 +11:00

42 lines
1.4 KiB
TypeScript

import type { UserIdentity } from '@supabase/supabase-js'
import { useQuery } from '@tanstack/react-query'
import { useUser } from 'common'
import { profileKeys } from './keys'
import { auth } from '@/lib/gotrue'
import { UseCustomQueryOptions } from '@/types'
export async function getProfileIdentities() {
// getUser() is used instead of getSession() here because the client is configured
// with a `userStorage` option, under which getSession() can return a session whose
// `user` is a placeholder that throws when any of its properties are read.
const { error, data } = await auth.getUser()
if (error) throw error
if (!data.user) throw new Error('User not found with getUser()')
const { identities = [], email, new_email, email_change_sent_at } = data.user
return { identities, email, new_email, email_change_sent_at }
}
type ProfileIdentitiesData = {
identities: (UserIdentity & { email?: string })[]
email?: string
new_email?: string
email_change_sent_at?: string
}
type ProfileIdentitiesError = any
export const useProfileIdentitiesQuery = <TData = ProfileIdentitiesData>({
enabled = true,
...options
}: UseCustomQueryOptions<ProfileIdentitiesData, ProfileIdentitiesError, TData> = {}) => {
const userId = useUser()?.id
return useQuery<ProfileIdentitiesData, ProfileIdentitiesError, TData>({
queryKey: profileKeys.identities(userId),
queryFn: () => getProfileIdentities(),
enabled,
...options,
})
}