@@ -37,33 +37,64 @@ function MFAThemeWrapper(props: WebAuthnMFAProps): JSX.Element {
37
37
export default MFAThemeWrapper ;
38
38
39
39
export function MFATheme ( props : WebAuthnMFAProps ) : JSX . Element {
40
- const { onBackButtonClicked, onSignIn } = props ;
41
- const [ activeScreen , setActiveScreen ] = React . useState < MFAScreens > ( MFAScreens . SignIn ) ;
42
- const [ signUpEmail , setSignUpEmail ] = React . useState < string > ( "" ) ;
43
40
const t = useTranslation ( ) ;
44
41
45
- const onRegisterPasskeyClick = React . useCallback ( ( ) => {
46
- if ( ! props . featureState . canRegisterPasskey ) {
47
- return ;
48
- }
49
- if ( props . featureState . email ) {
50
- setActiveScreen ( MFAScreens . SignUpConfirmation ) ;
51
- } else {
52
- setActiveScreen ( MFAScreens . SignUp ) ;
42
+ if ( ! props . featureState . loaded ) {
43
+ return < WebauthnMFALoadingScreen /> ;
44
+ }
45
+
46
+ if ( props . featureState . accessDenied ) {
47
+ return (
48
+ < AccessDeniedScreen
49
+ useShadowDom = { false /* We set this to false, because we are already inside a shadowDom (if required) */ }
50
+ error = { t ( props . featureState . error ! ) }
51
+ />
52
+ ) ;
53
+ }
54
+
55
+ return (
56
+ < div data-supertokens = "container webauthn-mfa" >
57
+ < div data-supertokens = "row" >
58
+ < MFAThemeRouter { ...props } />
59
+ </ div >
60
+ < SuperTokensBranding />
61
+ </ div >
62
+ ) ;
63
+ }
64
+
65
+ function MFAThemeRouter ( props : WebAuthnMFAProps ) : JSX . Element {
66
+ const { onBackButtonClicked, onSignIn } = props ;
67
+ const [ activeScreen , setActiveScreen ] = React . useState < MFAScreens > ( ( ) => {
68
+ if ( ! props . featureState . hasRegisteredPassKey ) {
69
+ return props . featureState . email ? MFAScreens . SignUpConfirmation : MFAScreens . SignUp ;
53
70
}
54
- } , [ props . featureState . email , props . featureState . canRegisterPasskey ] ) ;
71
+ return MFAScreens . SignIn ;
72
+ } ) ;
73
+ const [ email , setEmail ] = React . useState < string > ( "" ) ;
74
+ const signUpEmail = props . featureState . email || email ;
55
75
56
76
const onSignUpContinue = React . useCallback (
57
77
( email : string ) => {
58
78
if ( ! props . featureState . canRegisterPasskey ) {
59
79
return ;
60
80
}
61
81
setActiveScreen ( MFAScreens . SignUpConfirmation ) ;
62
- setSignUpEmail ( email ) ;
82
+ setEmail ( email ) ;
63
83
} ,
64
84
[ props . featureState . canRegisterPasskey ]
65
85
) ;
66
86
87
+ const onRegisterPasskeyClick = React . useCallback ( ( ) => {
88
+ if ( ! props . featureState . canRegisterPasskey ) {
89
+ return ;
90
+ }
91
+ if ( props . featureState . email ) {
92
+ setActiveScreen ( MFAScreens . SignUpConfirmation ) ;
93
+ } else {
94
+ setActiveScreen ( MFAScreens . SignUp ) ;
95
+ }
96
+ } , [ props . featureState . email , props . featureState . canRegisterPasskey ] ) ;
97
+
67
98
const clearError = React . useCallback ( ( ) => {
68
99
props . dispatch ( { type : "setError" , error : undefined } ) ;
69
100
} , [ props ] ) ;
@@ -75,71 +106,91 @@ export function MFATheme(props: WebAuthnMFAProps): JSX.Element {
75
106
[ props ]
76
107
) ;
77
108
78
- const onClickSignUpBackButton = React . useCallback ( ( ) => {
79
- if ( ! props . featureState . canRegisterPasskey ) {
109
+ const onClickSignUpConfirmationBackButton = React . useCallback ( ( ) => {
110
+ if ( ! props . featureState . email ) {
111
+ setActiveScreen ( MFAScreens . SignUp ) ;
112
+ return ;
113
+ }
114
+ if ( ! props . featureState . hasRegisteredPassKey && ! props . featureState . showBackButton ) {
115
+ return ;
116
+ }
117
+ if ( ! props . featureState . hasRegisteredPassKey ) {
118
+ onBackButtonClicked ( ) ;
80
119
return ;
81
120
}
82
121
setActiveScreen ( MFAScreens . SignIn ) ;
83
- } , [ props . featureState . canRegisterPasskey ] ) ;
122
+ } , [
123
+ props . featureState . email ,
124
+ props . featureState . hasRegisteredPassKey ,
125
+ props . featureState . showBackButton ,
126
+ onBackButtonClicked ,
127
+ ] ) ;
128
+
129
+ const showBackButtonOnSignUpConfirmation = React . useMemo ( ( ) => {
130
+ return (
131
+ ! ! props . featureState . email || props . featureState . hasRegisteredPassKey || props . featureState . showBackButton
132
+ ) ;
133
+ } , [ props . featureState . email , props . featureState . hasRegisteredPassKey , props . featureState . showBackButton ] ) ;
84
134
85
- const onClickSignUpConfirmationBackButton = React . useCallback ( ( ) => {
86
- if ( props . featureState . email ) {
87
- setActiveScreen ( MFAScreens . SignIn ) ;
88
- } else {
89
- setActiveScreen ( MFAScreens . SignUp ) ;
90
- }
91
- } , [ props . featureState . email ] ) ;
135
+ const onSignUp = React . useCallback ( async ( ) => {
136
+ await props . onSignUp ( signUpEmail ) ;
137
+ } , [ props . onSignUp , signUpEmail ] ) ;
92
138
93
139
const onFetchError = React . useCallback ( ( ) => {
94
140
onError ( "SOMETHING_WENT_WRONG_ERROR" ) ;
95
141
} , [ onError ] ) ;
96
142
97
- if ( ! props . featureState . loaded ) {
98
- return < WebauthnMFALoadingScreen /> ;
143
+ const onClickSignUpBackButton = React . useCallback ( ( ) => {
144
+ if ( ! props . featureState . hasRegisteredPassKey && ! props . featureState . showBackButton ) {
145
+ return ;
146
+ }
147
+ if ( ! props . featureState . hasRegisteredPassKey ) {
148
+ onBackButtonClicked ( ) ;
149
+ return ;
150
+ }
151
+ setActiveScreen ( MFAScreens . SignIn ) ;
152
+ } , [ props . featureState . hasRegisteredPassKey , props . featureState . showBackButton , onBackButtonClicked ] ) ;
153
+
154
+ const showBackButtonOnSignUp = React . useMemo ( ( ) => {
155
+ return props . featureState . hasRegisteredPassKey || props . featureState . showBackButton ;
156
+ } , [ props . featureState . email , props . featureState . showBackButton ] ) ;
157
+
158
+ if ( activeScreen === MFAScreens . SignUp ) {
159
+ return (
160
+ < WebauthnMFASignUp
161
+ clearError = { clearError }
162
+ onError = { onError }
163
+ onFetchError = { onFetchError }
164
+ error = { props . featureState . error }
165
+ onContinueClick = { onSignUpContinue }
166
+ email = { email }
167
+ onRecoverAccountClick = { props . onRecoverAccountClick }
168
+ onBackButtonClicked = { showBackButtonOnSignUp ? onClickSignUpBackButton : undefined }
169
+ />
170
+ ) ;
99
171
}
100
172
101
- if ( props . featureState . accessDenied ) {
173
+ if ( activeScreen === MFAScreens . SignUpConfirmation ) {
102
174
return (
103
- < AccessDeniedScreen
104
- useShadowDom = { false /* We set this to false, because we are already inside a shadowDom (if required) */ }
105
- error = { t ( props . featureState . error ! ) }
175
+ < WebauthnMFASignUpConfirmation
176
+ onSignUp = { onSignUp }
177
+ onBackButtonClicked = {
178
+ showBackButtonOnSignUpConfirmation ? onClickSignUpConfirmationBackButton : undefined
179
+ }
180
+ email = { signUpEmail }
181
+ error = { props . featureState . error }
106
182
/>
107
183
) ;
108
184
}
109
185
110
186
return (
111
- < div data-supertokens = "container webauthn-mfa" >
112
- < div data-supertokens = "row" >
113
- { activeScreen === MFAScreens . SignIn ? (
114
- < WebauthnMFASignIn
115
- onBackButtonClicked = { props . featureState . showBackButton ? onBackButtonClicked : undefined }
116
- canRegisterPasskey = { props . featureState . canRegisterPasskey }
117
- onSignIn = { onSignIn }
118
- error = { props . featureState . error }
119
- onRegisterPasskeyClick = { onRegisterPasskeyClick }
120
- deviceSupported = { props . featureState . deviceSupported }
121
- />
122
- ) : activeScreen === MFAScreens . SignUp ? (
123
- < WebauthnMFASignUp
124
- clearError = { clearError }
125
- onError = { onError }
126
- onFetchError = { onFetchError }
127
- error = { props . featureState . error }
128
- onContinueClick = { onSignUpContinue }
129
- email = { signUpEmail }
130
- onRecoverAccountClick = { props . onRecoverAccountClick }
131
- onBackButtonClicked = { onClickSignUpBackButton }
132
- />
133
- ) : (
134
- < WebauthnMFASignUpConfirmation
135
- onSignUp = { props . onSignUp }
136
- onBackButtonClicked = { onClickSignUpConfirmationBackButton }
137
- email = { props . featureState . email || signUpEmail }
138
- error = { props . featureState . error }
139
- />
140
- ) }
141
- </ div >
142
- < SuperTokensBranding />
143
- </ div >
187
+ < WebauthnMFASignIn
188
+ onBackButtonClicked = { props . featureState . showBackButton ? onBackButtonClicked : undefined }
189
+ canRegisterPasskey = { props . featureState . canRegisterPasskey }
190
+ onSignIn = { onSignIn }
191
+ error = { props . featureState . error }
192
+ deviceSupported = { props . featureState . deviceSupported }
193
+ onRegisterPasskeyClick = { onRegisterPasskeyClick }
194
+ />
144
195
) ;
145
196
}
0 commit comments