@@ -5,7 +5,7 @@ import { withCardStateProvider } from '@/ui/elements/contexts';
55import { LoadingCard } from '@/ui/elements/LoadingCard' ;
66
77import { useRouter } from '../../router' ;
8- import { determineStartingSignInSecondFactor , secondFactorKey } from '../SignIn/utils ' ;
8+ import { useSecondFactorSelection } from '../SignIn/useSecondFactorSelection ' ;
99import { secondFactorsAreEqual } from './useReverificationAlternativeStrategies' ;
1010import { UserVerificationFactorTwoTOTP } from './UserVerificationFactorTwoTOTP' ;
1111import { useUserVerificationSession , withUserVerificationSessionGuard } from './useUserVerificationSession' ;
@@ -33,27 +33,20 @@ export function UserVerificationFactorTwoComponent(): JSX.Element {
3333 ) ;
3434 } , [ sessionVerification . supportedSecondFactors ] ) ;
3535
36- const lastPreparedFactorKeyRef = React . useRef ( '' ) ;
37- const [ currentFactor , setCurrentFactor ] = React . useState < SessionVerificationSecondFactor | null > (
38- ( ) => determineStartingSignInSecondFactor ( availableFactors ) as SessionVerificationSecondFactor ,
39- ) ;
40- const [ showAllStrategies , setShowAllStrategies ] = React . useState < boolean > ( ! currentFactor ) ;
41- const toggleAllStrategies = ( ) => setShowAllStrategies ( s => ! s ) ;
36+ const {
37+ currentFactor,
38+ factorAlreadyPrepared,
39+ handleFactorPrepare,
40+ selectFactor,
41+ showAllStrategies,
42+ toggleAllStrategies,
43+ } = useSecondFactorSelection < SessionVerificationSecondFactor > ( availableFactors ) ;
4244
4345 const secondFactorsExcludingCurrent = useMemo (
4446 ( ) => availableFactors ?. filter ( factor => ! secondFactorsAreEqual ( factor , currentFactor ) ) ,
4547 [ availableFactors , currentFactor ] ,
4648 ) ;
4749
48- const handleFactorPrepare = ( ) => {
49- lastPreparedFactorKeyRef . current = secondFactorKey ( currentFactor ) ;
50- } ;
51-
52- const selectFactor = ( factor : SessionVerificationSecondFactor ) => {
53- setCurrentFactor ( factor ) ;
54- toggleAllStrategies ( ) ;
55- } ;
56-
5750 const hasAlternativeStrategies = useMemo (
5851 ( ) => ( secondFactorsExcludingCurrent && secondFactorsExcludingCurrent . length > 0 ) || false ,
5952 [ secondFactorsExcludingCurrent ] ,
@@ -84,7 +77,7 @@ export function UserVerificationFactorTwoComponent(): JSX.Element {
8477 case 'phone_code' :
8578 return (
8679 < UVFactorTwoPhoneCodeCard
87- factorAlreadyPrepared = { lastPreparedFactorKeyRef . current === secondFactorKey ( currentFactor ) }
80+ factorAlreadyPrepared = { factorAlreadyPrepared }
8881 onFactorPrepare = { handleFactorPrepare }
8982 factor = { currentFactor }
9083 onShowAlternativeMethodsClicked = { toggleAllStrategies }
@@ -94,7 +87,7 @@ export function UserVerificationFactorTwoComponent(): JSX.Element {
9487 case 'totp' :
9588 return (
9689 < UserVerificationFactorTwoTOTP
97- factorAlreadyPrepared = { lastPreparedFactorKeyRef . current === secondFactorKey ( currentFactor ) }
90+ factorAlreadyPrepared = { factorAlreadyPrepared }
9891 onFactorPrepare = { handleFactorPrepare }
9992 factor = { currentFactor }
10093 onShowAlternativeMethodsClicked = { toggleAllStrategies }
0 commit comments