From bb9a6431dfb5fd41ec34d39f50d7fb2fba93b103 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Sun, 2 Apr 2023 11:41:50 +0600 Subject: [PATCH] refactor: email login form --- src/components/Login/EmailLoginForm.tsx | 100 ++++++++++++++---------- 1 file changed, 60 insertions(+), 40 deletions(-) diff --git a/src/components/Login/EmailLoginForm.tsx b/src/components/Login/EmailLoginForm.tsx index a64aece6b..59e1ae0e0 100644 --- a/src/components/Login/EmailLoginForm.tsx +++ b/src/components/Login/EmailLoginForm.tsx @@ -6,16 +6,31 @@ import { TOKEN_COOKIE_NAME } from '../../lib/utils'; const EmailLoginForm: FunctionComponent<{}> = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); - const [showVerifiedError, setShowVerifiedError] = useState(false); - const [message, setMessage] = useState(''); - const [error, setError] = useState<{ + const [message, setMessage] = useState<{ + type: 'success' | 'error' | 'verification' | 'warning'; message: string; - status: number; } | null>(null); const handleFormSubmit = async (e: Event) => { e.preventDefault(); + // Check if the verification-email-sent-at is less than 5 seconds ago + const verificationEmailSentAt = localStorage.getItem( + 'verification-email-sent-at' + ); + + console.log(verificationEmailSentAt); + + if (verificationEmailSentAt) { + const now = new Date(); + if (Number(verificationEmailSentAt) > now.getTime()) { + return setMessage({ + type: 'warning', + message: 'Please wait before sending another verification email.', + }); + } + } + const res = await fetch('http://localhost:8080/v1-login', { method: 'POST', headers: { @@ -30,12 +45,11 @@ const EmailLoginForm: FunctionComponent<{}> = () => { // If the response isn't ok, we'll throw an error if (json.type === 'user_not_verified') { - setError(null); - setMessage(''); - setShowVerifiedError(true); - return; - } else { - setShowVerifiedError(false); + return setMessage({ + type: 'verification', + message: + 'Your account is not verified. Please click the verification link in your email. Or resend verification email.', + }); } if (json.token) { @@ -43,9 +57,9 @@ const EmailLoginForm: FunctionComponent<{}> = () => { Cookies.set(TOKEN_COOKIE_NAME, json.token); window.location.href = '/'; } else { - setError({ + setMessage({ + type: 'error', message: json.message, - status: res.status, }); } }; @@ -59,13 +73,10 @@ const EmailLoginForm: FunctionComponent<{}> = () => { if (verificationEmailSentAt) { const now = new Date(); if (Number(verificationEmailSentAt) > now.getTime()) { - setMessage(''); - setShowVerifiedError(false); - setError({ + return setMessage({ + type: 'warning', message: 'Please wait before sending another verification email.', - status: 429, }); - return; } } @@ -85,15 +96,16 @@ const EmailLoginForm: FunctionComponent<{}> = () => { // If the response isn't ok, we'll throw an error if (!res.ok) { - setError({ + return setMessage({ + type: 'error', message: json.message, - status: res.status, }); } - // If the response is ok, we'll set the token in a cookie - setShowVerifiedError(false); - setMessage('Verification instructions have been sent to your email.'); + setMessage({ + type: 'success', + message: 'Verification instructions have been sent to your email.', + }); // Current time + 5 seconds, save it to localStorage const now = new Date(); @@ -137,24 +149,32 @@ const EmailLoginForm: FunctionComponent<{}> = () => { onChange={(e) => setPassword(String((e.target as any).value))} /> - {error && ( -
{error.message}
- )} - - {message &&
{message}
} - - {showVerifiedError && ( -
- Your account is not verified. Please click the verification link in - your email. Or{' '} - -
+ {message && ( + <> + {message.type === 'verification' ? ( +
+ Your account is not verified. Please click the verification link + in your email. Or{' '} + +
+ ) : ( +
+ {message.message} +
+ )} + )}