Skip to content

Commit 5b1419f

Browse files
committed
Refine forget password functionality
1 parent 43d20ea commit 5b1419f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

frontend/src/pages/ForgetPassword/index.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import { toast } from "react-toastify";
1414
import {
1515
PASSWORD_REQUIRED_ERROR_MESSAGE,
1616
PASSWORD_MISMATCH_ERROR_MESSAGE,
17+
TOKEN_REQUIRED_ERROR_MESSAGE,
1718
} from "../../utils/constants";
1819

1920
const ForgetPassword: React.FC = () => {
2021
const navigate = useNavigate();
2122

2223
const [email, setEmail] = useState<string>("");
2324
const [hasSentEmail, setHasSentEmail] = useState<boolean>(false);
25+
const [isLoading, setisLoading] = useState<boolean>(false);
2426

2527
const {
2628
register: registerEmail,
@@ -38,6 +40,7 @@ const ForgetPassword: React.FC = () => {
3840
});
3941

4042
const handleSendEmail = async (email: string) => {
43+
setisLoading(true);
4144
userClient
4245
.post("/users/send-reset-password-email", { email })
4346
.then((res) => {
@@ -47,10 +50,14 @@ const ForgetPassword: React.FC = () => {
4750
})
4851
.catch((err) => {
4952
toast.error(err.response?.data.message || err.message);
53+
})
54+
.finally(() => {
55+
setisLoading(false);
5056
});
5157
};
5258

5359
const handleResetPassword = async (password: string, token: string) => {
60+
setisLoading(true);
5461
userClient
5562
.post(`/users/reset-password`, { email, token, password })
5663
.then((res) => {
@@ -59,6 +66,9 @@ const ForgetPassword: React.FC = () => {
5966
})
6067
.catch((err) => {
6168
toast.error(err.response?.data.message || err.message);
69+
})
70+
.finally(() => {
71+
setisLoading(false);
6272
});
6373
};
6474

@@ -99,6 +109,7 @@ const ForgetPassword: React.FC = () => {
99109
sx={(theme) => ({ marginTop: theme.spacing(1) })}
100110
{...registerPassword("token", {
101111
setValueAs: (value: string) => value.trim(),
112+
required: TOKEN_REQUIRED_ERROR_MESSAGE,
102113
})}
103114
error={!!errorsPassword.token}
104115
helperText={errorsPassword.token?.message}
@@ -148,10 +159,16 @@ const ForgetPassword: React.FC = () => {
148159
onClick={() => {
149160
handleSendEmail(email);
150161
}}
162+
disabled={isLoading}
151163
>
152164
Resend
153165
</Button>
154-
<Button fullWidth variant="contained" type="submit">
166+
<Button
167+
fullWidth
168+
variant="contained"
169+
type="submit"
170+
disabled={isLoading}
171+
>
155172
Change Password
156173
</Button>
157174
</Stack>
@@ -220,6 +237,7 @@ const ForgetPassword: React.FC = () => {
220237
type="submit"
221238
variant="contained"
222239
sx={(theme) => ({ margin: theme.spacing(2, 0) })}
240+
disabled={isLoading}
223241
>
224242
Send Reset Link
225243
</Button>

frontend/src/utils/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export const PASSWORD_SPECIAL_CHAR_ERROR_MESSAGE =
5050
export const PASSWORD_WEAK_ERROR_MESSAGE = "Password is weak";
5151
export const PASSWORD_MISMATCH_ERROR_MESSAGE = "Password does not match";
5252

53+
/* Token Validation */
54+
export const TOKEN_REQUIRED_ERROR_MESSAGE = "Token is required";
55+
5356
/* Toast Messages */
5457
// Authentication
5558
export const SUCCESS_LOG_OUT = "Logged out successfully!";

0 commit comments

Comments
 (0)