Files
supabase/apps/studio/hooks/misc/useAccountRecovery.ts
Danny White 5cf3cf1032 feat(studio): make security the home for sign-in and recovery setup (#52358)
## Problem

- Sign-in methods are in Preferences, separate from MFA and recovery
settings.
- The risk of a single sign-in method (e.g. GitHub, and getting locked
out of that account) isn’t clearly communicated.

## Solution

- Bring sign-in methods, MFA and recovery codes together on the Security
page. If Security is disabled, sign-in methods remain accessible in
Preferences.
- Highlight the most important next step with a primary button. For
example, when MFA is off, “Set up MFA” takes priority over adding a
password.
- Explain the risk of relying on a single sign-in provider, and make it
clear which email address a new password will apply to.
- After connecting a sign-in provider, return users to the settings page
they can access. Links to password setup open the password dialog
directly.

| After |
| --- |
| <img width="1460" height="1426" alt="CleanShot 2026-11-06 at 14 54
23@2x"
src="https://github.com/user-attachments/assets/afb82601-1b6f-43be-9e98-a0edb8d529c8"
/> |
| _The “Sign-in methods” section moved to Account → Security. Note also
the new Admonition regarding the risk of a single (GitHub) sign-in
method._ |

## Review instructions

1. Open
[Security](https://studio-staging-git-dnywh-recovery-security-home-supabase.vercel.app/dashboard/account/security#sign-in-methods).
Check sign-in methods appear above MFA and recovery codes. With MFA off,
“Set up MFA” should be primary.
2. Open Preferences. Sign-in methods should be absent unless Security is
disabled.
3. On a test account, check password setup, email editing and provider
linking. [Open password setup
directly](https://studio-staging-git-dnywh-recovery-security-home-supabase.vercel.app/dashboard/account/security?securityAction=set-password#sign-in-methods),
then cancel and confirm it stays closed.
2026-11-09 17:42:43 +11:00

78 lines
2.7 KiB
TypeScript

import { useFlag, useUser } from 'common'
import { useMfaListFactorsQuery } from '@/data/profile/mfa-list-factors-query'
import { useProfileIdentitiesQuery } from '@/data/profile/profile-identities-query'
import { useRecoveryCodesStatusQuery } from '@/data/recovery-codes/recovery-codes-status-query'
import {
evaluateAccountRecovery,
type AccountRecoveryInput,
} from '@/lib/account-recovery/evaluate-account-recovery'
import { IS_PLATFORM } from '@/lib/constants'
export const useAccountRecovery = ({ enabled = true }: { enabled?: boolean } = {}) => {
const userId = useUser()?.id
const recoveryCodesEnabled = useFlag('enableAuthRecoveryCodes')
const passwordEnabled = useFlag('enableAccountPassword')
const shouldReadAccount = IS_PLATFORM && enabled && !!userId
const identities = useProfileIdentitiesQuery({ enabled: shouldReadAccount })
const factors = useMfaListFactorsQuery({ enabled: shouldReadAccount })
const verifiedFactorCount = factors.isSuccess
? factors.data.all.filter(
(factor) => factor.status === 'verified' && factor.factor_type !== 'recovery_code'
).length
: 0
const isSsoAccount =
identities.isSuccess &&
identities.data.identities.some((identity) => identity.provider.startsWith('sso'))
const shouldReadCodes =
shouldReadAccount &&
recoveryCodesEnabled &&
factors.isSuccess &&
verifiedFactorCount > 0 &&
identities.isSuccess &&
!isSsoAccount
const codes = useRecoveryCodesStatusQuery({ enabled: shouldReadCodes })
const isPending =
shouldReadAccount &&
(identities.isPending || factors.isPending || (shouldReadCodes && codes.isPending))
const isError =
shouldReadAccount &&
(identities.isError || factors.isError || (shouldReadCodes && codes.isError))
let recoveryCodes: AccountRecoveryInput['recoveryCodes'] = { status: 'unknown' }
if (shouldReadCodes && codes.isSuccess) {
if (codes.data.status === 'unenrolled') recoveryCodes = { status: 'missing' }
if (codes.data.status === 'available' && codes.data.data) {
recoveryCodes = { status: 'available', remaining: codes.data.data.remaining }
}
}
const findings =
shouldReadAccount && identities.isSuccess
? evaluateAccountRecovery({
identities: identities.data.identities,
email: identities.data.email,
verifiedFactorCount,
recoveryCodes,
features: {
recoveryCodesEnabled,
passwordEnabled,
},
})
: []
const isFetching =
shouldReadAccount &&
(identities.isFetching || factors.isFetching || (shouldReadCodes && codes.isFetching))
return {
findings,
isPending,
isFetching,
isError,
verifiedFactorCount,
isFactorsSuccess: shouldReadAccount && factors.isSuccess,
}
}