Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #38

Merged
merged 2 commits into from
Jan 3, 2024
Merged

Dev #38

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function App() {
<Route path="/register" element={<Register />} />
<Route path="/otp-request-container" element={<OtpRequest />} />
<Route path="/reset-code" element={<ResetCode />} />
<Route path="/reset-password" element={<ResetPassword />} />
<Route path="/reset-password/:email" element={<ResetPassword />} />
<Route path="/movie/:id" element={<MoviePage />} />
<Route path="/booking/:id" element={<Booking />} />
<Route path="/seating/:showTimeId/:id" element={<Seating />} />
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/ForgotPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const ForgotPassword = () => {
);
// Handle the response based on your API's specification

navigate("/reset-password");
navigate(`/reset-password/${email}`);
Swal.fire(
"Success",
"OTP request has been sent successfully.",
Expand Down
56 changes: 53 additions & 3 deletions client/src/components/ResetPassword.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useRef } from "react";
import { useNavigate } from "react-router-dom";
import React, { useState, useRef, useEffect } from "react";
import { useNavigate, useParams } from "react-router-dom";
import Swal from "sweetalert2";
import axios from "axios";
import "./css/ResetPassword.css";
Expand All @@ -9,15 +9,62 @@ import { useLoading } from "./LoadingContext.js";

const ResetPassword = () => {
const [otp, setOtp] = useState("");
const { email } = useParams();
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [error, setError] = useState("");
const [timeLeft, setTimeLeft] = useState(10);
const otpRef = useRef(null);
const passwordRef = useRef(null);
const confirmPasswordRef = useRef(null);
const navigate = useNavigate();
const { loading, setLoading } = useLoading();

useEffect(() => {
if (!timeLeft) {
Swal.fire({
title: "Time's up",
text: "You ran out of time to reset your password",
icon: "info",
showCancelButton: true,
confirmButtonText: "Retry",
cancelButtonText: "Go to Login",
}).then((result) => {
if (result.isConfirmed) {
requestNewOTP();
} else {
navigate("/login-container");
}
});
return;
}

const intervalId = setInterval(() => {
setTimeLeft(timeLeft - 1);
}, 1000);

return () => clearInterval(intervalId);
}, [timeLeft, navigate]);

const requestNewOTP = async () => {
try {
setLoading(true);
await axios.post(
`${process.env.REACT_APP_API_PATH}/auth/password-reset-request`,
{ email }
);
setLoading(false);
Swal.fire(
"OTP Sent",
"A new OTP has been sent to your email.",
"success"
);
setTimeLeft(300); // Reset timer
} catch (error) {
Swal.fire("Error", "There was a problem requesting a new OTP.", "error");
}
};

const handleSubmit = async (event) => {
event.preventDefault();
setError("");
Expand All @@ -42,7 +89,6 @@ const ResetPassword = () => {
newPassword: password,
}
);
// Handle the response based on your API's specification
Swal.fire("Success", "Password has been reset successfully.", "success");
navigate("/login-container");
} catch (error) {
Expand Down Expand Up @@ -84,6 +130,10 @@ const ResetPassword = () => {
>
<form onSubmit={handleSubmit}>
<h1>Reset Your Password</h1>
<div className="timer">
Time remaining to reset password: {Math.floor(timeLeft / 60)}:
{("0" + (timeLeft % 60)).slice(-2)}
</div>
<input
ref={otpRef}
type="text"
Expand Down
10 changes: 7 additions & 3 deletions client/src/components/css/ResetPassword.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@

.reset-password .error {
color: red;
font-size: 1em;
margin-bottom: 1rem;
font-size: 1em;
margin-bottom: 1rem;
text-align: center;
display: block;
display: block;
overflow: hidden;
height: 1em;
line-height: 1em;
Expand All @@ -73,3 +73,7 @@
font-size: 28px;
}
}
.timer {
text-align: center;
padding: 10px;
}
Loading