Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Send OTP button for Verify OTP UI Flow #235

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions packages/react/src/components/Auth/interfaces/VerifyOtp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,46 @@ function VerifyOtp({
const [token, setToken] = useState('')
const [error, setError] = useState('')
const [message, setMessage] = useState('')
const [loading, setLoading] = useState(false)
const [loadingOtp, setLoadingOtp] = useState(false)
const [loadingVerify, setLoadingVerify] = useState(false)

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
setError('')
setMessage('')
setLoading(true)
setLoadingVerify(true)

let verifyOpts: VerifyOtpParams = {
let verifyOtps: VerifyOtpParams = {
email,
token,
type: otpType as EmailOtpType,
}
if (['sms', 'phone_change'].includes(otpType)) {
verifyOpts = {
verifyOtps = {
phone,
token,
type: otpType as MobileOtpType,
}
}
const { error } = await supabaseClient.auth.verifyOtp(verifyOpts)
const { error } = await supabaseClient.auth.verifyOtp(verifyOtps)
if (error) setError(error.message)
setLoading(false)
setLoadingVerify(false)
}

const sendOtp = async () => {
setLoadingOtp(true)

let verifyOtps: VerifyOtpParams = {
email,
}
if (['sms', 'phone_change'].includes(otpType)) {
verifyOtps = {
phone,
}
}
const { error } = await supabaseClient.auth.signInWithOtp(verifyOtps)
if (error) setError(error.message)
setLoadingOtp(false)
}

const labels = i18n?.verify_otp
Expand Down Expand Up @@ -101,6 +118,14 @@ function VerifyOtp({
/>
</div>
)}
<Button
color="secondary"
onClick={sendOtp}
loading={loadingOtp}
appearance={appearance}
>
{loadingOtp ? labels?.sending_button_label : labels?.send_button_label}
</Button>
<div>
<Label htmlFor="token" appearance={appearance}>
{labels?.token_input_label}
Expand All @@ -119,10 +144,10 @@ function VerifyOtp({
<Button
color="primary"
type="submit"
loading={loading}
loading={loadingVerify}
appearance={appearance}
>
{loading ? labels?.loading_button_label : labels?.button_label}
{loadingVerify ? labels?.loading_button_label : labels?.button_label}
</Button>
{showLinks && (
<Anchor
Expand Down
4 changes: 3 additions & 1 deletion packages/shared/src/localization/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"token_input_label": "Token",
"token_input_placeholder": "Your Otp token",
"button_label": "Verify token",
"loading_button_label": "Signing in ..."
"loading_button_label": "Signing in ...",
"send_button_label": "Send OTP",
"sending_button_label": "Sending OTP ...",
}
}
2 changes: 2 additions & 0 deletions packages/shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,7 @@ export type I18nVariables = {
token_input_placeholder?: string
button_label?: string
loading_button_label?: string
send_button_label?: string
sending_button_label?: string
}
}