Files
supabase/apps/studio/data/profile/mfa-authenticator-assurance-level-query.ts
Danny WhiteandJoshen Lim 1052d414ad fix(studio): verify identity before removing authenticators (#52352)
## Problem

Authenticator removal can fail when the session has not completed MFA.

This is difficult to test (see below) but shows an awkward toast with
unclear “AAL1” and ”AAL2” copy, which is unhelpful to the user.

## Solution

Checks the session before removal confirmation. If needed, shows _Verify
your identity_ first.

Depends on #52351. Badge presentation: #52748. Recovery-code
verification: #52747.

| After |
| --- |
| <img width="814" height="725" alt="Security Account Supabase"
src="https://github.com/user-attachments/assets/8d0dc002-3777-4ba5-a4bc-9d98a059c2c4"
/> |
| _Unfilled state._ |
| <img width="814" height="725" alt="63727"
src="https://github.com/user-attachments/assets/5472de19-99b6-4403-aaca-f92e319445cb"
/> |
| _Errored state._ |
| <img width="814" height="725" alt="27157"
src="https://github.com/user-attachments/assets/f8480f0b-5cc4-432f-b38f-79cd311274cf"
/> |
| _What the above (success state) unlocks: being able to remove
authenticator._ |

## Review instructions

Use a disposable MFA account and sign in normally.

1. Open [Security locally](http://localhost:8082/account/security) (so
the breakpoint feature can be used). Click Delete beside an
authenticator. Since you already entered an MFA code at sign-in, removal
confirmation should open directly. Cancel keeps the authenticator. Click
Cancel.
2. To show verification, open DevTools → Sources → ⌘P. Find
`mfa-authenticator-assurance-level-query.ts`. Inside
`getMfaAuthenticatorAssuranceLevel`, click the line number beside
**`return data`** to add a breakpoint, then (back in the Studio
interface) click Delete.
3. When Paused in debugger appears, press Esc for the Console drawer (in
DevTools) and run `data.currentLevel = 'aal1'`. Remove the breakpoint by
clicking its line number again, then press F8 or Resume ▶ (at the top of
Studio in the yellow strip).
4. Try an invalid authenticator code, then a valid one. Verification
must lead to a separate removal confirmation. Cancel must keep the
authenticator.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-11-09 16:35:31 +11:00

52 lines
1.5 KiB
TypeScript

import type { AuthMFAGetAuthenticatorAssuranceLevelResponse } from '@supabase/supabase-js'
import { useQuery, type QueryKey } from '@tanstack/react-query'
import { useUser } from 'common'
import { profileKeys } from './keys'
import type { Profile } from './types'
import { auth } from '@/lib/gotrue'
import { UseCustomQueryOptions } from '@/types'
export type ProfileResponse = Profile
export async function getMfaAuthenticatorAssuranceLevel() {
const { error, data } = await auth.mfa.getAuthenticatorAssuranceLevel()
if (error) throw error
return data
}
type CustomAuthMFAGetAuthenticatorAssuranceLevelData = NonNullable<
AuthMFAGetAuthenticatorAssuranceLevelResponse['data']
>
type CustomAuthMFAGetAuthenticatorAssuranceLevelError = NonNullable<
AuthMFAGetAuthenticatorAssuranceLevelResponse['error']
>
export const useAuthenticatorAssuranceLevelQuery = <
TData = CustomAuthMFAGetAuthenticatorAssuranceLevelData,
>({
enabled = true,
queryKey,
...options
}: UseCustomQueryOptions<
CustomAuthMFAGetAuthenticatorAssuranceLevelData,
CustomAuthMFAGetAuthenticatorAssuranceLevelError,
TData
> & {
queryKey?: QueryKey
} = {}) => {
const userId = useUser()?.id
return useQuery<
CustomAuthMFAGetAuthenticatorAssuranceLevelData,
CustomAuthMFAGetAuthenticatorAssuranceLevelError,
TData
>({
queryKey: queryKey ?? profileKeys.aaLevel(userId),
enabled,
queryFn: () => getMfaAuthenticatorAssuranceLevel(),
staleTime: 1000 * 60 * 30,
...options,
})
}