@@ -15,7 +15,7 @@ import { supabase } from "@/integrations/supabase/client";
1515import { useAuth } from "@/hooks/use-auth" ;
1616import { toast } from "sonner" ;
1717
18- type Step = "idle" | "enable-password" | "enable- qr" | "enable-complete" ;
18+ type Step = "idle" | "enable-qr" | "enable-complete" ;
1919
2020export function SecurityTab ( {
2121 requiredSetup = false ,
@@ -32,8 +32,6 @@ export function SecurityTab({
3232 const [ qrCode , setQrCode ] = useState ( "" ) ;
3333 const [ secret , setSecret ] = useState ( "" ) ;
3434 const [ totpCode , setTotpCode ] = useState ( "" ) ;
35- const [ password , setPassword ] = useState ( "" ) ;
36- const [ showPassword , setShowPassword ] = useState ( false ) ;
3735 const [ busy , setBusy ] = useState ( false ) ;
3836 const [ secretCopied , setSecretCopied ] = useState ( false ) ;
3937
@@ -54,35 +52,21 @@ export function SecurityTab({
5452
5553 const reset = ( ) => {
5654 setStep ( "idle" ) ;
57- setPassword ( "" ) ;
5855 setTotpCode ( "" ) ;
5956 setQrCode ( "" ) ;
6057 setSecret ( "" ) ;
6158 setEnrollFactorId ( "" ) ;
62- setShowPassword ( false ) ;
6359 setSecretCopied ( false ) ;
6460 setBusy ( false ) ;
6561 } ;
6662
67- const verifyPassword = async ( pw : string ) : Promise < boolean > => {
68- if ( ! user ?. email ) return false ;
69- const { error } = await supabase . auth . signInWithPassword ( {
70- email : user . email ,
71- password : pw ,
72- } ) ;
73- return ! error ;
74- } ;
75-
76- const handleEnablePassword = async ( ) => {
77- if ( ! password ) {
78- toast . error ( "Enter your password." ) ;
79- return ;
80- }
81-
63+ const handleStartEnrollment = async ( ) => {
8264 setBusy ( true ) ;
83- const ok = await verifyPassword ( password ) ;
84- if ( ! ok ) {
85- toast . error ( "Incorrect password." ) ;
65+
66+ const { data : assurance , error : assuranceError } =
67+ await supabase . auth . mfa . getAuthenticatorAssuranceLevel ( ) ;
68+ if ( assuranceError || assurance . currentLevel !== "aal2" ) {
69+ toast . error ( "Two-step verification is required before adding a backup authenticator." ) ;
8670 setBusy ( false ) ;
8771 return ;
8872 }
@@ -109,7 +93,6 @@ export function SecurityTab({
10993 setEnrollFactorId ( data . id ) ;
11094 setQrCode ( data . totp . qr_code ) ;
11195 setSecret ( data . totp . secret ) ;
112- setPassword ( "" ) ;
11396 setBusy ( false ) ;
11497 setStep ( "enable-qr" ) ;
11598 } ;
@@ -199,7 +182,7 @@ export function SecurityTab({
199182 ? "rounded-xl"
200183 : "rounded-xl bg-gradient-to-r from-violet to-electric text-white"
201184 }
202- onClick = { ( ) => setStep ( "enable-password" ) }
185+ onClick = { ( ) => void handleStartEnrollment ( ) }
203186 >
204187 { mfaCount > 0 ? (
205188 < >
@@ -221,33 +204,6 @@ export function SecurityTab({
221204 </ p >
222205 </ div >
223206
224- { step === "enable-password" && (
225- < StepCard
226- icon = { < KeyRound className = "h-5 w-5 text-violet" /> }
227- title = { mfaCount > 0 ? "Add Backup Authenticator" : "Verify Your Password" }
228- description = "Re-enter your current password before creating a new TOTP factor."
229- onClose = { reset }
230- >
231- < PasswordField
232- value = { password }
233- show = { showPassword }
234- onChange = { setPassword }
235- onToggle = { ( ) => setShowPassword ( ( visible ) => ! visible ) }
236- />
237- < div className = "flex gap-2" >
238- < Button
239- onClick = { ( ) => void handleEnablePassword ( ) }
240- disabled = { busy || ! password }
241- className = "rounded-xl bg-gradient-to-r from-violet to-electric text-white"
242- >
243- { busy ? "Verifying…" : "Continue" }
244- </ Button >
245- < Button variant = "outline" className = "rounded-xl" onClick = { reset } >
246- Cancel
247- </ Button >
248- </ div >
249- </ StepCard >
250- ) }
251207
252208 { step === "enable-qr" && (
253209 < StepCard
@@ -391,41 +347,3 @@ function StepCard({
391347 ) ;
392348}
393349
394- function PasswordField ( {
395- value,
396- show,
397- onChange,
398- onToggle,
399- } : {
400- value : string ;
401- show : boolean ;
402- onChange : ( value : string ) => void ;
403- onToggle : ( ) => void ;
404- } ) {
405- return (
406- < div >
407- < Label htmlFor = "security-password" className = "text-xs" >
408- Current Password
409- </ Label >
410- < div className = "relative mt-1.5" >
411- < Input
412- id = "security-password"
413- type = { show ? "text" : "password" }
414- value = { value }
415- onChange = { ( event ) => onChange ( event . target . value ) }
416- className = "rounded-xl pr-10"
417- autoComplete = "current-password"
418- autoFocus
419- />
420- < button
421- type = "button"
422- onClick = { onToggle }
423- className = "absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
424- aria-label = { show ? "Hide password" : "Show password" }
425- >
426- { show ? < EyeOff className = "h-4 w-4" /> : < Eye className = "h-4 w-4" /> }
427- </ button >
428- </ div >
429- </ div >
430- ) ;
431- }
0 commit comments