@@ -10,15 +10,20 @@ type LoginResponse = {
1010
1111export default function LoginPage ( ) {
1212
13+ const [ emailButtonAllow , setEmailButtonAllow ] = useState < boolean > ( true )
14+
1315 const otpOnClick = async ( ) => {
1416
17+ setEmailButtonAllow ( false )
18+
1519 const email = ( document . getElementById ( 'email' ) as HTMLInputElement ) . value
1620
1721 if ( ! / ^ [ a - z A - Z 0 - 9 . _ % + - ] + @ [ a - z A - Z 0 - 9 . - ] + \. [ a - z A - Z ] { 2 , } $ / . test ( email ) ) {
1822 setMessage ( {
1923 className : "text-red-500 mb-4 text-center" ,
2024 message : "Please enter a valid email address."
2125 } )
26+ setEmailButtonAllow ( true )
2227 return ;
2328 } else {
2429 setMessage ( {
@@ -46,6 +51,7 @@ export default function LoginPage() {
4651 message : `An error occurred. Please try again. (code ${ response . status } )`
4752 } )
4853 }
54+ setEmailButtonAllow ( true )
4955 }
5056
5157 const router = useRouter ( )
@@ -60,29 +66,31 @@ export default function LoginPage() {
6066 message : "Please enter a valid 6-digit OTP."
6167 } )
6268 return ;
63- } else {
64- setMessage ( {
65- className : "hidden" ,
66- message : ""
67- } )
6869 }
70+ setMessage ( {
71+ className : "hidden" ,
72+ message : ""
73+ } )
6974
7075 const otp = parseInt ( otpStr )
7176
72- const res = await fetch ( apiUrl `api/v1/auth/validate_otp` , {
77+ const response = await fetch ( apiUrl `api/v1/auth/validate_otp` , {
7378 method : 'POST' ,
74- headers : {
75- 'Content-Type' : 'application/json' ,
76- } ,
77- body : JSON . stringify ( { email, otp } ) ,
79+ credentials : 'include' ,
80+ body : JSON . stringify ( { email, otp } )
7881 } )
79-
80- if ( res . status === 200 ) {
82+ const data = await response . json ( )
83+ if ( response . ok ) {
8184 router . push ( '/dashboard' )
82- } else if ( res . status = == 401 ) {
85+ } else if ( response . status == 401 ) {
8386 setMessage ( {
8487 className : "text-red-500 mb-4 text-center" ,
85- message : "Invalid OTP. Please try again."
88+ message : "Wrong OTP. Please try again."
89+ } )
90+ } else {
91+ setMessage ( {
92+ className : "text-red-500 mb-4 text-center" ,
93+ message : data [ "detail" ] || `An error occurred. Please try again. (code ${ response . status } )`
8694 } )
8795 }
8896 }
@@ -109,6 +117,7 @@ export default function LoginPage() {
109117 />
110118 < button className = "mt-4 px-4 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 w-full"
111119 onClick = { otpOnClick }
120+ disabled = { ! emailButtonAllow }
112121 >
113122 Send OTP
114123 </ button >
@@ -117,14 +126,14 @@ export default function LoginPage() {
117126 < input type = "number"
118127 id = "otp"
119128 name = "otp"
120- className = { "bg-gray-100 border-gray-400 text-gray-900 text-2xl text-center rounded-lg w-full [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none [&::-moz-appearance:textfield]" }
129+ className = { "bg-gray-100 border-gray-400 text-gray-900 text-2xl text-center rounded-lg w-full appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none [&::-moz-appearance:textfield]" }
121130 placeholder = "674169"
122131
123132 />
124- < button className = "mt-4 px-4 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 w-full"
133+ < button className = "mt-4 px-4 py-2 bg-green-400 text-white rounded-lg hover:bg-green-300 w-full"
125134 onClick = { validateOnClick }
126135 >
127- Send OTP
136+ Confirm
128137 </ button >
129138 </ div >
130139 </ div >
0 commit comments