-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2b6ac9
commit 123f7f9
Showing
9 changed files
with
147 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
import React, { FC, useEffect, useState } from 'react'; | ||
import isEmail from 'validator/es/lib/isEmail'; | ||
import isMobilePhone from 'validator/es/lib/isMobilePhone'; | ||
|
||
import styles from '../styles/default.css'; | ||
import { ButtonAppearance, MessageType, Views } from '../constants'; | ||
import { useAuthorizer } from '../contexts/AuthorizerContext'; | ||
import { StyledButton, StyledFooter, StyledLink } from '../styledComponents'; | ||
import { formatErrorMessage } from '../utils/format'; | ||
import { Message } from './Message'; | ||
import { AuthorizerVerifyOtp } from './AuthorizerVerifyOtp'; | ||
import { OtpDataType } from '../types'; | ||
|
||
interface InputDataType { | ||
email: string | null; | ||
email_or_phone_number: string | null; | ||
} | ||
|
||
const initOtpData: OtpDataType = { | ||
is_screen_visible: false, | ||
email: '', | ||
phone_number: '', | ||
}; | ||
|
||
export const AuthorizerForgotPassword: FC<{ | ||
setView?: (v: Views) => void; | ||
onForgotPassword?: (data: any) => void; | ||
|
@@ -20,11 +29,12 @@ export const AuthorizerForgotPassword: FC<{ | |
const [error, setError] = useState(``); | ||
const [loading, setLoading] = useState(false); | ||
const [successMessage, setSuccessMessage] = useState(``); | ||
const [otpData, setOtpData] = useState<OtpDataType>({ ...initOtpData }); | ||
const [formData, setFormData] = useState<InputDataType>({ | ||
email: null, | ||
email_or_phone_number: null, | ||
}); | ||
const [errorData, setErrorData] = useState<InputDataType>({ | ||
email: null, | ||
email_or_phone_number: null, | ||
}); | ||
const { authorizerRef, config } = useAuthorizer(); | ||
|
||
|
@@ -36,22 +46,50 @@ export const AuthorizerForgotPassword: FC<{ | |
e.preventDefault(); | ||
try { | ||
setLoading(true); | ||
|
||
const res = await authorizerRef.forgotPassword({ | ||
email: formData.email || '', | ||
let email: string = ''; | ||
let phone_number: string = ''; | ||
if (formData.email_or_phone_number) { | ||
if (isEmail(formData.email_or_phone_number)) { | ||
email = formData.email_or_phone_number; | ||
} else if (isMobilePhone(formData.email_or_phone_number)) { | ||
phone_number = formData.email_or_phone_number; | ||
} | ||
} | ||
if (!email && !phone_number) { | ||
setErrorData({ | ||
...errorData, | ||
email_or_phone_number: 'Invalid email or phone number', | ||
}); | ||
setLoading(false); | ||
return; | ||
} | ||
const { data: res, errors } = await authorizerRef.forgotPassword({ | ||
email: email, | ||
phone_number: phone_number, | ||
state: urlProps?.state || '', | ||
redirect_uri: | ||
urlProps?.redirect_uri || | ||
config.redirectURL || | ||
window.location.origin, | ||
}); | ||
setLoading(false); | ||
|
||
if (res && res.message) { | ||
if (errors && errors.length) { | ||
setError(formatErrorMessage(errors[0]?.message)); | ||
return; | ||
} | ||
if (res?.message) { | ||
setError(``); | ||
setSuccessMessage(res.message); | ||
if (res?.should_show_mobile_otp_screen) { | ||
setOtpData({ | ||
...otpData, | ||
is_screen_visible: true, | ||
email: email, | ||
phone_number: phone_number, | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
if (onForgotPassword) { | ||
onForgotPassword(res); | ||
} | ||
|
@@ -66,19 +104,43 @@ export const AuthorizerForgotPassword: FC<{ | |
}; | ||
|
||
useEffect(() => { | ||
if (formData.email === '') { | ||
setErrorData({ ...errorData, email: 'Email is required' }); | ||
} else if (formData.email && !isEmail(formData.email)) { | ||
setErrorData({ ...errorData, email: 'Please enter valid email' }); | ||
if (formData.email_or_phone_number === '') { | ||
setErrorData({ | ||
...errorData, | ||
email_or_phone_number: 'Email OR Phone Number is required', | ||
}); | ||
} else if ( | ||
!isEmail(formData.email_or_phone_number || '') && | ||
!isMobilePhone(formData.email_or_phone_number || '') | ||
) { | ||
setErrorData({ | ||
...errorData, | ||
email_or_phone_number: 'Invalid Email OR Phone Number', | ||
}); | ||
} else { | ||
setErrorData({ ...errorData, email: null }); | ||
setErrorData({ ...errorData, email_or_phone_number: null }); | ||
} | ||
}, [formData.email]); | ||
}, [formData.email_or_phone_number]); | ||
|
||
if (successMessage) { | ||
return <Message type={MessageType.Success} text={successMessage} />; | ||
} | ||
|
||
if (otpData.is_screen_visible) { | ||
return ( | ||
<AuthorizerVerifyOtp | ||
email={otpData.email} | ||
phone_number={otpData.phone_number} | ||
urlProps={urlProps} | ||
onLogin={() => { | ||
if (onForgotPassword) { | ||
onForgotPassword({}); | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{error && ( | ||
|
@@ -93,29 +155,39 @@ export const AuthorizerForgotPassword: FC<{ | |
<div className={styles['styled-form-group']}> | ||
<label | ||
className={styles['form-input-label']} | ||
htmlFor="authorizer-forgot-password-email" | ||
htmlFor="authorizer-forgot-password-email-or-phone-number" | ||
> | ||
<span>* </span>Email | ||
</label> | ||
<input | ||
name="email" | ||
id="authorizer-forgot-password-email" | ||
name="email_or_phone_number" | ||
id="authorizer-forgot-password-email-or-" | ||
className={`${styles['form-input-field']} ${ | ||
errorData.email ? styles['input-error-content'] : null | ||
errorData.email_or_phone_number | ||
? styles['input-error-content'] | ||
: null | ||
}`} | ||
placeholder="eg. [email protected]" | ||
type="email" | ||
value={formData.email || ''} | ||
onChange={(e) => onInputChange('email', e.target.value)} | ||
type="text" | ||
value={formData.email_or_phone_number || ''} | ||
onChange={(e) => | ||
onInputChange('email_or_phone_number', e.target.value) | ||
} | ||
/> | ||
{errorData.email && ( | ||
<div className={styles['form-input-error']}>{errorData.email}</div> | ||
{errorData.email_or_phone_number && ( | ||
<div className={styles['form-input-error']}> | ||
{errorData.email_or_phone_number} | ||
</div> | ||
)} | ||
</div> | ||
<br /> | ||
<StyledButton | ||
type="submit" | ||
disabled={loading || !!errorData.email || !formData.email} | ||
disabled={ | ||
loading || | ||
!!errorData.email_or_phone_number || | ||
!formData.email_or_phone_number | ||
} | ||
appearance={ButtonAppearance.Primary} | ||
> | ||
{loading ? `Processing ...` : `Send Email`} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters