Skip to content

Commit

Permalink
belinda-nextjs-bug-269-error-message-display-inconsistent (#270)
Browse files Browse the repository at this point in the history
* fix error messages

* add reroute the user to signin page afterward
  • Loading branch information
tinpham5614 authored Mar 18, 2024
1 parent 341a5d5 commit 20f4c00
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions app/auth/change-password-page/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ChangeEventHandler, FormEventHandler, useState } from "react";
import Visibility from "@mui/icons-material/Visibility";
import VisibilityOff from "@mui/icons-material/VisibilityOff";
import {
Alert,
Box,
Button,
CircularProgress,
Expand All @@ -16,6 +15,9 @@ import {
TextField,
Typography,
} from "@mui/material";
import ErrorAlert from "@/components/ErrorAlert";
import SuccessAlert from "@/components/SuccessAlert";
import { useRouter } from "next/navigation";

const ChangePasswordPage = () => {
const [password, setPassword] = useState({
Expand Down Expand Up @@ -44,6 +46,9 @@ const ChangePasswordPage = () => {
setShowConfirmPassword(!showConfirmPassword);
};

// router
const router = useRouter();

// handle change event
const handleChange: ChangeEventHandler<HTMLInputElement> = ({ target }) => {
const { name, value } = target;
Expand All @@ -67,6 +72,12 @@ const ChangePasswordPage = () => {
return;
}

// Check if password is less than 8 characters
if (newPassword.length < 8) {
setError("Password must be at least 8 characters long");
return;
}

// token after login
const token = localStorage.getItem("token") || null;

Expand All @@ -83,30 +94,27 @@ const ChangePasswordPage = () => {
"http://localhost:3000/api/auth/change-password",
{
method: "POST",
body: JSON.stringify({
newPassword,
confirmPassword,
}),
body: JSON.stringify(password),
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
}
);
if (!res.ok) {
setError(error || "Failed to change password");
const { message } = await res.json();
setError(message);
setSuccess("");
} else {
// reset the form
setPassword({ newPassword: "", confirmPassword: "" });
setSuccess("Password changed successfully");
setError("");
}
return;
}
setError("");
//set delay to show success message
setSuccess("Password changed successfully, redirecting to sign in page");
setTimeout(() => {
router.push("/auth/sign-in");
}, 3000);
} catch (error) {
console.error("Error changing password:", error);
setError("Failed to change password");
setLoading(false);
setSuccess("");
} finally {
setLoading(false);
}
Expand Down Expand Up @@ -206,16 +214,8 @@ const ChangePasswordPage = () => {
),
}}
/>
{error && (
<Alert severity="error" sx={{ mb: 2 }}>
{error}
</Alert>
)}
{success && (
<Alert severity="success" sx={{ mb: 2 }}>
{success}
</Alert>
)}
{error && <ErrorAlert message={error} />}
{success && <SuccessAlert message={success} />}
<Button
type="submit"
fullWidth
Expand Down

0 comments on commit 20f4c00

Please sign in to comment.