11import React , { FC , useEffect , useState } from 'react' ;
22import { AuthToken , SignupInput } from '@authorizerdev/authorizer-js' ;
3- import styles from '../styles/default.css' ;
3+ import isEmail from 'validator/es/lib/isEmail' ;
4+ import isMobilePhone from 'validator/es/lib/isMobilePhone' ;
45
6+ import styles from '../styles/default.css' ;
57import { ButtonAppearance , MessageType , Views } from '../constants' ;
68import { useAuthorizer } from '../contexts/AuthorizerContext' ;
79import { StyledButton , StyledFooter , StyledLink } from '../styledComponents' ;
8- import { isValidEmail } from '../utils/validations' ;
910import { formatErrorMessage } from '../utils/format' ;
1011import { Message } from './Message' ;
1112import PasswordStrengthIndicator from './PasswordStrengthIndicator' ;
1213
1314interface InputDataType {
14- email : string | null ;
15+ email_or_phone_number : string | null ;
1516 password : string | null ;
1617 confirmPassword : string | null ;
1718}
@@ -26,12 +27,12 @@ export const AuthorizerSignup: FC<{
2627 const [ loading , setLoading ] = useState ( false ) ;
2728 const [ successMessage , setSuccessMessage ] = useState ( `` ) ;
2829 const [ formData , setFormData ] = useState < InputDataType > ( {
29- email : null ,
30+ email_or_phone_number : null ,
3031 password : null ,
3132 confirmPassword : null ,
3233 } ) ;
3334 const [ errorData , setErrorData ] = useState < InputDataType > ( {
34- email : null ,
35+ email_or_phone_number : null ,
3536 password : null ,
3637 confirmPassword : null ,
3738 } ) ;
@@ -46,8 +47,26 @@ export const AuthorizerSignup: FC<{
4647 e . preventDefault ( ) ;
4748 try {
4849 setLoading ( true ) ;
50+ let email : string = '' ;
51+ let phone_number : string = '' ;
52+ if ( formData . email_or_phone_number ) {
53+ if ( isEmail ( formData . email_or_phone_number ) ) {
54+ email = formData . email_or_phone_number ;
55+ } else if ( isMobilePhone ( formData . email_or_phone_number ) ) {
56+ phone_number = formData . email_or_phone_number ;
57+ }
58+ }
59+ if ( ! email && ! phone_number ) {
60+ setErrorData ( {
61+ ...errorData ,
62+ email_or_phone_number : 'Invalid email or phone number' ,
63+ } ) ;
64+ setLoading ( false ) ;
65+ return ;
66+ }
4967 const data : SignupInput = {
50- email : formData . email || '' ,
68+ email : email ,
69+ phone_number : phone_number ,
5170 password : formData . password || '' ,
5271 confirm_password : formData . confirmPassword || '' ,
5372 } ;
@@ -103,14 +122,23 @@ export const AuthorizerSignup: FC<{
103122 } ;
104123
105124 useEffect ( ( ) => {
106- if ( formData . email === '' ) {
107- setErrorData ( { ...errorData , email : 'Email is required' } ) ;
108- } else if ( formData . email && ! isValidEmail ( formData . email ) ) {
109- setErrorData ( { ...errorData , email : 'Please enter valid email' } ) ;
125+ if ( formData . email_or_phone_number === '' ) {
126+ setErrorData ( {
127+ ...errorData ,
128+ email_or_phone_number : 'Email OR Phone Number is required' ,
129+ } ) ;
130+ } else if (
131+ ! isEmail ( formData . email_or_phone_number || '' ) &&
132+ ! isMobilePhone ( formData . email_or_phone_number || '' )
133+ ) {
134+ setErrorData ( {
135+ ...errorData ,
136+ email_or_phone_number : 'Invalid Email OR Phone Number' ,
137+ } ) ;
110138 } else {
111- setErrorData ( { ...errorData , email : null } ) ;
139+ setErrorData ( { ...errorData , email_or_phone_number : null } ) ;
112140 }
113- } , [ formData . email ] ) ;
141+ } , [ formData . email_or_phone_number ] ) ;
114142
115143 useEffect ( ( ) => {
116144 if ( formData . password === '' ) {
@@ -158,7 +186,8 @@ export const AuthorizerSignup: FC<{
158186 { error && (
159187 < Message type = { MessageType . Error } text = { error } onClose = { onErrorClose } />
160188 ) }
161- { config . is_basic_authentication_enabled &&
189+ { ( config . is_basic_authentication_enabled ||
190+ config . is_mobile_basic_authentication_enabled ) &&
162191 ! config . is_magic_link_login_enabled && (
163192 < >
164193 < form onSubmit = { onSubmit } name = "authorizer-sign-up-form" >
@@ -167,22 +196,24 @@ export const AuthorizerSignup: FC<{
167196 className = { styles [ 'form-input-label' ] }
168197 htmlFor = "authorizer-sign-up-email"
169198 >
170- < span > * </ span > Email
199+ < span > * </ span > Email / Phone Number
171200 </ label >
172201 < input
173- name = "email "
174- id = "authorizer-sign-up- email"
202+ name = "eemail_or_phone_numbermail "
203+ id = "authorizer-login- email-or-phone-number "
175204 className = { `${ styles [ 'form-input-field' ] } ${
176- errorData . email ? styles [ 'input-error-content' ] : null
205+ errorData . email_or_phone_number
206+ ? styles [ 'input-error-content' ]
207+ : null
177208 } `}
178- placeholder = "eg. foo@bar .com"
179- type = "email "
180- value = { formData . email || '' }
209+ placeholder = "eg. hello@world .com / +919999999999 "
210+ type = "text "
211+ value = { formData . email_or_phone_number || '' }
181212 onChange = { ( e ) => onInputChange ( 'email' , e . target . value ) }
182213 />
183- { errorData . email && (
214+ { errorData . email_or_phone_number && (
184215 < div className = { styles [ 'form-input-error' ] } >
185- { errorData . email }
216+ { errorData . email_or_phone_number }
186217 </ div >
187218 ) }
188219 </ div >
@@ -253,10 +284,10 @@ export const AuthorizerSignup: FC<{
253284 disabled = {
254285 loading ||
255286 disableSignupButton ||
256- ! ! errorData . email ||
287+ ! ! errorData . email_or_phone_number ||
257288 ! ! errorData . password ||
258289 ! ! errorData . confirmPassword ||
259- ! formData . email ||
290+ ! formData . email_or_phone_number ||
260291 ! formData . password ||
261292 ! formData . confirmPassword
262293 }
0 commit comments