1
1
import type { SessionVerificationResource , SessionVerificationSecondFactor , SignInFactor } from '@clerk/types' ;
2
- import React , { useEffect } from 'react' ;
2
+ import React , { useEffect , useMemo } from 'react' ;
3
3
4
4
import { LoadingCard , withCardStateProvider } from '../../elements' ;
5
5
import { useRouter } from '../../router' ;
6
6
import { determineStartingSignInSecondFactor } from '../SignIn/utils' ;
7
+ import { secondFactorsAreEqual } from './useReverificationAlternativeStrategies' ;
7
8
import { UserVerificationFactorTwoTOTP } from './UserVerificationFactorTwoTOTP' ;
8
9
import { useUserVerificationSession , withUserVerificationSessionGuard } from './useUserVerificationSession' ;
9
10
import { UVFactorTwoAlternativeMethods } from './UVFactorTwoAlternativeMethods' ;
@@ -21,7 +22,7 @@ const factorKey = (factor: SignInFactor | null | undefined) => {
21
22
return key ;
22
23
} ;
23
24
24
- export function _UserVerificationFactorTwo ( ) : JSX . Element {
25
+ export function UserVerificationFactorTwoComponent ( ) : JSX . Element {
25
26
const { navigate } = useRouter ( ) ;
26
27
const { data } = useUserVerificationSession ( ) ;
27
28
const sessionVerification = data as SessionVerificationResource ;
@@ -35,6 +36,11 @@ export function _UserVerificationFactorTwo(): JSX.Element {
35
36
const [ showAllStrategies , setShowAllStrategies ] = React . useState < boolean > ( ! currentFactor ) ;
36
37
const toggleAllStrategies = ( ) => setShowAllStrategies ( s => ! s ) ;
37
38
39
+ const secondFactorsExcludingCurrent = useMemo (
40
+ ( ) => availableFactors ?. filter ( factor => ! secondFactorsAreEqual ( factor , currentFactor ) ) ,
41
+ [ availableFactors , currentFactor ] ,
42
+ ) ;
43
+
38
44
const handleFactorPrepare = ( ) => {
39
45
lastPreparedFactorKeyRef . current = factorKey ( currentFactor ) ;
40
46
} ;
@@ -44,20 +50,26 @@ export function _UserVerificationFactorTwo(): JSX.Element {
44
50
toggleAllStrategies ( ) ;
45
51
} ;
46
52
53
+ const hasAlternativeStrategies = useMemo (
54
+ ( ) => ( secondFactorsExcludingCurrent && secondFactorsExcludingCurrent . length > 0 ) || false ,
55
+ [ secondFactorsExcludingCurrent ] ,
56
+ ) ;
57
+
47
58
useEffect ( ( ) => {
48
59
if ( sessionVerification . status === 'needs_first_factor' ) {
49
60
void navigate ( '../' ) ;
50
61
}
62
+ // eslint-disable-next-line react-hooks/exhaustive-deps
51
63
} , [ ] ) ;
52
64
53
65
if ( ! currentFactor ) {
54
66
return < LoadingCard /> ;
55
67
}
56
68
57
- if ( showAllStrategies ) {
69
+ if ( showAllStrategies && hasAlternativeStrategies ) {
58
70
return (
59
71
< UVFactorTwoAlternativeMethods
60
- supportedSecondFactors = { sessionVerification . supportedSecondFactors }
72
+ supportedSecondFactors = { secondFactorsExcludingCurrent || null }
61
73
onBackLinkClick = { toggleAllStrategies }
62
74
onFactorSelected = { selectFactor }
63
75
/>
@@ -72,6 +84,7 @@ export function _UserVerificationFactorTwo(): JSX.Element {
72
84
onFactorPrepare = { handleFactorPrepare }
73
85
factor = { currentFactor }
74
86
onShowAlternativeMethodsClicked = { toggleAllStrategies }
87
+ showAlternativeMethods = { hasAlternativeStrategies }
75
88
/>
76
89
) ;
77
90
case 'totp' :
@@ -81,6 +94,7 @@ export function _UserVerificationFactorTwo(): JSX.Element {
81
94
onFactorPrepare = { handleFactorPrepare }
82
95
factor = { currentFactor }
83
96
onShowAlternativeMethodsClicked = { toggleAllStrategies }
97
+ showAlternativeMethods = { hasAlternativeStrategies }
84
98
/>
85
99
) ;
86
100
case 'backup_code' :
@@ -91,5 +105,5 @@ export function _UserVerificationFactorTwo(): JSX.Element {
91
105
}
92
106
93
107
export const UserVerificationFactorTwo = withUserVerificationSessionGuard (
94
- withCardStateProvider ( _UserVerificationFactorTwo ) ,
108
+ withCardStateProvider ( UserVerificationFactorTwoComponent ) ,
95
109
) ;
0 commit comments