141 lines
5.6 KiB
TypeScript
141 lines
5.6 KiB
TypeScript
import { useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { Mail, Lock, } from "lucide-react";
|
|
import { CustomInput, CustomButton, CustomOTPInput, CustomBackButton } from "../../../components/custom";
|
|
import { authApi } from "../AuthApi";
|
|
|
|
type Step = "email" | "otp" | "password";
|
|
|
|
export default function ResetPassword() {
|
|
const [step, setStep] = useState<Step>("email");
|
|
const [email, setEmail] = useState("");
|
|
const [otp, setOtp] = useState("");
|
|
const [newPassword, setNewPassword] = useState("");
|
|
const [confirmPassword, setConfirmPassword] = useState("");
|
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const navigate = useNavigate();
|
|
|
|
const handleSendOtp = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setIsLoading(true);
|
|
try {
|
|
await authApi.forgotPassword(email);
|
|
setStep("otp");
|
|
} catch (error) {
|
|
console.error(error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleVerifyOtp = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setIsLoading(true);
|
|
try {
|
|
await authApi.verifyOtp(email, otp);
|
|
setStep("password");
|
|
} catch (error) {
|
|
console.error(error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleResetPassword = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (newPassword !== confirmPassword) {
|
|
alert("Passwords do not match");
|
|
return;
|
|
}
|
|
setIsLoading(true);
|
|
try {
|
|
await authApi.resetPasswordWithOtp(email, otp, newPassword);
|
|
navigate("/signin");
|
|
} catch (error) {
|
|
console.error(error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col flex-1 [&_.text-gray-700]:!text-white">
|
|
<div className="w-full max-w-md pt-10 mx-auto">
|
|
<CustomBackButton to="/signin" tooltip="Back to Sign In" className="text-gray-400 hover:text-gray-200" />
|
|
</div>
|
|
<div className="flex flex-col justify-center flex-1 w-full max-w-md mx-auto">
|
|
<div className="mb-5 sm:mb-8">
|
|
<h1 className="mb-2 font-semibold text-white text-2xl">
|
|
{step === "email" && "Forgot Password?"}
|
|
{step === "otp" && "Verify OTP"}
|
|
{step === "password" && "Reset Password"}
|
|
</h1>
|
|
<p className="text-sm text-gray-300">
|
|
{step === "email" && "Enter your email address to receive a verification code."}
|
|
{step === "otp" && `Enter the verification code sent to ${email}`}
|
|
{step === "password" && "Enter your new password below."}
|
|
</p>
|
|
</div>
|
|
|
|
<form onSubmit={step === "email" ? handleSendOtp : step === "otp" ? handleVerifyOtp : handleResetPassword}>
|
|
<div className="space-y-6">
|
|
{step === "email" && (
|
|
<CustomInput
|
|
label="Email Address"
|
|
type="email"
|
|
placeholder="Enter your email"
|
|
required
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
leftIcon={<Mail size={20} />}
|
|
/>
|
|
)}
|
|
|
|
{step === "otp" && (
|
|
<CustomOTPInput
|
|
value={otp}
|
|
onChange={setOtp}
|
|
label="OTP Code"
|
|
length={6}
|
|
/>
|
|
)}
|
|
|
|
{step === "password" && (
|
|
<>
|
|
<CustomInput
|
|
label="New Password"
|
|
type="password"
|
|
placeholder="Enter new password"
|
|
required
|
|
value={newPassword}
|
|
onChange={(e) => setNewPassword(e.target.value)}
|
|
leftIcon={<Lock size={20} />}
|
|
/>
|
|
<CustomInput
|
|
label="Confirm Password"
|
|
type="password"
|
|
placeholder="Confirm new password"
|
|
required
|
|
value={confirmPassword}
|
|
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
leftIcon={<Lock size={20} />}
|
|
/>
|
|
</>
|
|
)}
|
|
|
|
<div>
|
|
<CustomButton className="w-full" size="md" variant="primary" type="submit" loading={isLoading}>
|
|
{step === "email" && "Send OTP"}
|
|
{step === "otp" && "Verify"}
|
|
{step === "password" && "Reset Password"}
|
|
</CustomButton>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|