Refactor AuthRedirect component to use useEffect for redirection logic and update Login component to handle OTP submission more flexibly. Adjusted OTP input handling to trigger submission on reaching the required length, improving user experience during login.
This commit is contained in:
@@ -1,19 +1,27 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
|
// React Imports
|
||||||
|
import { useEffect } from 'react'
|
||||||
|
|
||||||
// Next Imports
|
// Next Imports
|
||||||
import { redirect, usePathname } from 'next/navigation'
|
import { usePathname, useRouter } from 'next/navigation'
|
||||||
|
|
||||||
// Config Imports
|
// Config Imports
|
||||||
import themeConfig from '@configs/themeConfig'
|
import themeConfig from '@configs/themeConfig'
|
||||||
|
|
||||||
const AuthRedirect = () => {
|
const AuthRedirect = () => {
|
||||||
const pathname = usePathname()
|
const pathname = usePathname()
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
const redirectUrl = `/login?redirectTo=${pathname}`
|
useEffect(() => {
|
||||||
const login = `/login`
|
const login = '/login'
|
||||||
const homePage = themeConfig.homePageUrl
|
const homePage = themeConfig.homePageUrl
|
||||||
|
const redirectUrl =
|
||||||
|
pathname === login ? login : pathname === homePage ? login : `/login?redirectTo=${pathname}`
|
||||||
|
router.replace(redirectUrl)
|
||||||
|
}, [pathname, router])
|
||||||
|
|
||||||
return redirect(pathname === login ? login : pathname === homePage ? login : redirectUrl)
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
export default AuthRedirect
|
export default AuthRedirect
|
||||||
|
|||||||
+10
-6
@@ -155,8 +155,9 @@ const Login = ({ mode }: { mode: SystemMode }) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const onOtpSubmit = async () => {
|
const onOtpSubmit = async (otpValue?: string) => {
|
||||||
if (otp.length !== 6) {
|
const code = otpValue ?? otp
|
||||||
|
if (code.length !== 6) {
|
||||||
setErrorState({ message: t('errors.incompleteOtp') })
|
setErrorState({ message: t('errors.incompleteOtp') })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -166,7 +167,7 @@ const Login = ({ mode }: { mode: SystemMode }) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const phoneNumber = getValues('phone_number')
|
const phoneNumber = getValues('phone_number')
|
||||||
await login(phoneNumber, otp, tempToken)
|
await login(phoneNumber, code, tempToken)
|
||||||
|
|
||||||
// Redirect on successful login
|
// Redirect on successful login
|
||||||
const redirectURL = searchParams.get('redirectTo') ?? '/'
|
const redirectURL = searchParams.get('redirectTo') ?? '/'
|
||||||
@@ -265,9 +266,12 @@ const Login = ({ mode }: { mode: SystemMode }) => {
|
|||||||
<OtpContainer>
|
<OtpContainer>
|
||||||
<OTPInput
|
<OTPInput
|
||||||
value={otp}
|
value={otp}
|
||||||
onChange={setOtp}
|
onChange={(value) => {
|
||||||
|
setOtp(value)
|
||||||
|
if (value.length === 6) onOtpSubmit(value)
|
||||||
|
}}
|
||||||
maxLength={6}
|
maxLength={6}
|
||||||
containerClassName='flex gap-2'
|
containerClassName='flex gap-2 ltr:flex-row rtl:flex-row-reverse'
|
||||||
render={({ slots }) => (
|
render={({ slots }) => (
|
||||||
<>
|
<>
|
||||||
{slots.map((slot, idx) => (
|
{slots.map((slot, idx) => (
|
||||||
@@ -290,7 +294,7 @@ const Login = ({ mode }: { mode: SystemMode }) => {
|
|||||||
/>
|
/>
|
||||||
</OtpContainer>
|
</OtpContainer>
|
||||||
</div>
|
</div>
|
||||||
<Button fullWidth variant='contained' onClick={onOtpSubmit} disabled={isLoading || otp.length !== 6}>
|
<Button fullWidth variant='contained' onClick={() => onOtpSubmit()} disabled={isLoading || otp.length !== 6}>
|
||||||
{isLoading ? <CircularProgress size={24} /> : t('verifyOtp')}
|
{isLoading ? <CircularProgress size={24} /> : t('verifyOtp')}
|
||||||
</Button>
|
</Button>
|
||||||
<Button fullWidth variant='text' onClick={handleBackToPhone} disabled={isLoading}>
|
<Button fullWidth variant='text' onClick={handleBackToPhone} disabled={isLoading}>
|
||||||
|
|||||||
Reference in New Issue
Block a user