@@ -8,15 +8,26 @@ import { StyledButton, StyledFooter, StyledLink } from '../styledComponents';
88import { isValidEmail } from '../utils/validations' ;
99import { Message } from './Message' ;
1010import { AuthorizerVerifyOtp } from './AuthorizerVerifyOtp' ;
11- import { OtpDataType } from '../types' ;
11+ import { OtpDataType , TotpDataType } from '../types' ;
12+ import { AuthorizerTOTPScanner } from './AuthorizerTOTPScanner' ;
1213
1314const initOtpData : OtpDataType = {
1415 isScreenVisible : false ,
1516 email : '' ,
17+ phone_number : '' ,
18+ } ;
19+
20+ const initTotpData : TotpDataType = {
21+ isScreenVisible : false ,
22+ email : '' ,
23+ phone_number : '' ,
24+ authenticator_scanner_image : '' ,
25+ authenticator_secret : '' ,
26+ authenticator_recovery_codes : [ ] ,
1627} ;
1728
1829interface InputDataType {
19- email : string | null ;
30+ email_or_phone_number : string | null ;
2031 password : string | null ;
2132}
2233
@@ -29,12 +40,13 @@ export const AuthorizerBasicAuthLogin: FC<{
2940 const [ error , setError ] = useState ( `` ) ;
3041 const [ loading , setLoading ] = useState ( false ) ;
3142 const [ otpData , setOtpData ] = useState < OtpDataType > ( { ...initOtpData } ) ;
43+ const [ totpData , setTotpData ] = useState < TotpDataType > ( { ...initTotpData } ) ;
3244 const [ formData , setFormData ] = useState < InputDataType > ( {
33- email : null ,
45+ email_or_phone_number : null ,
3446 password : null ,
3547 } ) ;
3648 const [ errorData , setErrorData ] = useState < InputDataType > ( {
37- email : null ,
49+ email_or_phone_number : null ,
3850 password : null ,
3951 } ) ;
4052 const { setAuthData, config, authorizerRef } = useAuthorizer ( ) ;
@@ -48,7 +60,8 @@ export const AuthorizerBasicAuthLogin: FC<{
4860 setLoading ( true ) ;
4961 try {
5062 const data : LoginInput = {
51- email : formData . email || '' ,
63+ email : formData . email_or_phone_number || '' ,
64+ phone_number : formData . email_or_phone_number || '' ,
5265 password : formData . password || '' ,
5366 } ;
5467 if ( urlProps ?. scope ) {
@@ -63,11 +76,34 @@ export const AuthorizerBasicAuthLogin: FC<{
6376 }
6477
6578 const res = await authorizerRef . login ( data ) ;
66-
67- if ( res && res ?. should_show_email_otp_screen ) {
79+ // if totp is enabled for the first time show totp screen with scanner
80+ if (
81+ res &&
82+ res . should_show_totp_screen &&
83+ res . authenticator_scanner_image &&
84+ res . authenticator_secret &&
85+ res . authenticator_recovery_codes
86+ ) {
87+ setTotpData ( {
88+ isScreenVisible : true ,
89+ email : data . email || `` ,
90+ phone_number : data . phone_number || `` ,
91+ authenticator_scanner_image : res . authenticator_scanner_image ,
92+ authenticator_secret : res . authenticator_secret ,
93+ authenticator_recovery_codes : res . authenticator_recovery_codes ,
94+ } ) ;
95+ return ;
96+ }
97+ if (
98+ res &&
99+ ( res ?. should_show_email_otp_screen ||
100+ res ?. should_show_mobile_otp_screen ||
101+ res ?. should_show_totp_screen )
102+ ) {
68103 setOtpData ( {
69104 isScreenVisible : true ,
70- email : data . email ,
105+ email : data . email || `` ,
106+ phone_number : data . phone_number || `` ,
71107 } ) ;
72108 return ;
73109 }
@@ -101,14 +137,23 @@ export const AuthorizerBasicAuthLogin: FC<{
101137 } ;
102138
103139 useEffect ( ( ) => {
104- if ( formData . email === '' ) {
105- setErrorData ( { ...errorData , email : 'Email is required' } ) ;
106- } else if ( formData . email && ! isValidEmail ( formData . email ) ) {
107- setErrorData ( { ...errorData , email : 'Please enter valid email' } ) ;
140+ if ( formData . email_or_phone_number === '' ) {
141+ setErrorData ( {
142+ ...errorData ,
143+ email_or_phone_number : 'Email OR Phone Number is required' ,
144+ } ) ;
145+ } else if (
146+ formData . email_or_phone_number &&
147+ ! isValidEmail ( formData . email_or_phone_number )
148+ ) {
149+ setErrorData ( {
150+ ...errorData ,
151+ email_or_phone_number : 'Please enter valid email' ,
152+ } ) ;
108153 } else {
109- setErrorData ( { ...errorData , email : null } ) ;
154+ setErrorData ( { ...errorData , email_or_phone_number : null } ) ;
110155 }
111- } , [ formData . email ] ) ;
156+ } , [ formData . email_or_phone_number ] ) ;
112157
113158 useEffect ( ( ) => {
114159 if ( formData . password === '' ) {
@@ -118,12 +163,39 @@ export const AuthorizerBasicAuthLogin: FC<{
118163 }
119164 } , [ formData . password ] ) ;
120165
121- return otpData . isScreenVisible ? (
122- < AuthorizerVerifyOtp
123- { ...{ setView, onLogin, email : otpData . email } }
124- urlProps = { urlProps }
125- />
126- ) : (
166+ if ( totpData . isScreenVisible ) {
167+ return (
168+ < AuthorizerTOTPScanner
169+ { ...{
170+ setView,
171+ onLogin,
172+ email : totpData . email || `` ,
173+ phone_number : totpData . phone_number || `` ,
174+ authenticator_scanner_image : totpData . authenticator_scanner_image ,
175+ authenticator_secret : totpData . authenticator_secret ,
176+ authenticator_recovery_codes :
177+ totpData . authenticator_recovery_codes || [ ] ,
178+ } }
179+ urlProps = { urlProps }
180+ />
181+ ) ;
182+ }
183+
184+ if ( otpData . isScreenVisible ) {
185+ return (
186+ < AuthorizerVerifyOtp
187+ { ...{
188+ setView,
189+ onLogin,
190+ email : otpData . email || `` ,
191+ phone_number : otpData . phone_number || `` ,
192+ } }
193+ urlProps = { urlProps }
194+ />
195+ ) ;
196+ }
197+
198+ return (
127199 < >
128200 { error && (
129201 < Message type = { MessageType . Error } text = { error } onClose = { onErrorClose } />
@@ -138,19 +210,23 @@ export const AuthorizerBasicAuthLogin: FC<{
138210 < span > * </ span > Email
139211 </ label >
140212 < input
141- name = "email "
142- id = "authorizer-login-email"
213+ name = "email_or_phone_number "
214+ id = "authorizer-login-email-or-phone-number "
143215 className = { `${ styles [ 'form-input-field' ] } ${
144- errorData . email ? styles [ 'input-error-content' ] : null
216+ errorData . email_or_phone_number
217+ ? styles [ 'input-error-content' ]
218+ : null
145219 } `}
146- placeholder = "eg. foo@bar.com"
147- type = "email"
148- value = { formData . email || '' }
149- onChange = { ( e ) => onInputChange ( 'email' , e . target . value ) }
220+ placeholder = "eg. foo@bar.com / +919999999999"
221+ type = "text"
222+ value = { formData . email_or_phone_number || '' }
223+ onChange = { ( e ) =>
224+ onInputChange ( 'email_or_phone_number' , e . target . value )
225+ }
150226 />
151- { errorData . email && (
227+ { errorData . email_or_phone_number && (
152228 < div className = { styles [ 'form-input-error' ] } >
153- { errorData . email }
229+ { errorData . email_or_phone_number }
154230 </ div >
155231 ) }
156232 </ div >
@@ -182,9 +258,9 @@ export const AuthorizerBasicAuthLogin: FC<{
182258 < StyledButton
183259 type = "submit"
184260 disabled = {
185- ! ! errorData . email ||
261+ ! ! errorData . email_or_phone_number ||
186262 ! ! errorData . password ||
187- ! formData . email ||
263+ ! formData . email_or_phone_number ||
188264 ! formData . password ||
189265 loading
190266 }
0 commit comments