386 lines
10 KiB
TypeScript
386 lines
10 KiB
TypeScript
import React, { useState, useEffect } from "react";
|
||
import ImageWithBasePath from "../../core/common/imageWithBasePath";
|
||
import { Link, useNavigate } from "react-router-dom";
|
||
import { all_routes } from "../router/all_routes";
|
||
import { forgotPassword } from "../../api/usermanager";
|
||
import { toast } from "react-toastify";
|
||
|
||
const ForgotPassword = () => {
|
||
useEffect(() => {
|
||
const styleId = "animated-badge-keyframes";
|
||
if (!document.getElementById(styleId)) {
|
||
const styleSheet = document.createElement("style");
|
||
styleSheet.id = styleId;
|
||
styleSheet.innerText = `
|
||
@keyframes shine {
|
||
0% {
|
||
transform: skewX(-20deg) translateX(-150%);
|
||
}
|
||
50% {
|
||
transform: skewX(-20deg) translateX(150%);
|
||
}
|
||
100% {
|
||
transform: skewX(-20deg) translateX(150%);
|
||
}
|
||
}
|
||
`;
|
||
document.head.appendChild(styleSheet);
|
||
}
|
||
}, []);
|
||
|
||
const [email, setEmail] = useState("");
|
||
const [emailError, setEmailError] = useState("");
|
||
const [loading, setLoading] = useState(false);
|
||
const [touched, setTouched] = useState(false);
|
||
const navigate = useNavigate();
|
||
|
||
const validateEmail = (value: string) => {
|
||
const trimmed = value.trim();
|
||
|
||
// 1. Required
|
||
if (!trimmed) {
|
||
return "Email is required";
|
||
}
|
||
|
||
// 2. Max length
|
||
if (trimmed.length > 100) {
|
||
return "Email must not exceed 100 characters";
|
||
}
|
||
|
||
// 3. No consecutive spaces
|
||
if (/\s{2,}/.test(value)) {
|
||
return "Email cannot contain consecutive spaces";
|
||
}
|
||
|
||
// 4. No HTML or script tags
|
||
if (/<[^>]*>/g.test(value)) {
|
||
return "Email cannot contain HTML or script tags";
|
||
}
|
||
|
||
// 5. Only allowed characters (letters, numbers, allowed symbols)
|
||
const allowedCharsRegex = /^[A-Za-z0-9@$&\/\-_. ,]+$/;
|
||
if (!allowedCharsRegex.test(value)) {
|
||
return "Email contains invalid special characters";
|
||
}
|
||
|
||
// 6. Cannot be numbers only
|
||
if (/^\d+$/.test(trimmed)) {
|
||
return "Email cannot contain only numbers";
|
||
}
|
||
|
||
// 7. Cannot be symbols only
|
||
if (/^[^A-Za-z0-9]+$/.test(trimmed)) {
|
||
return "Email cannot contain only symbols";
|
||
}
|
||
|
||
// 8. No excessive repeated characters (e.g. aaaaa, ..... )
|
||
if (/(.)\1{4,}/.test(trimmed)) {
|
||
return "Email contains too many repeated characters";
|
||
}
|
||
|
||
// 9. Email format (FINAL CHECK)
|
||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||
if (!emailRegex.test(trimmed)) {
|
||
return "Enter a valid email address";
|
||
}
|
||
|
||
const blockedDomains = [
|
||
"tempmail.com",
|
||
"10minutemail.com",
|
||
"mailinator.com"
|
||
];
|
||
|
||
const domain = trimmed.split("@")[1]?.toLowerCase();
|
||
if (blockedDomains.includes(domain)) {
|
||
return "Temporary email addresses are not allowed";
|
||
}
|
||
|
||
const localPart = trimmed.split("@")[0];
|
||
if (/^[._-]|[._-]$/.test(localPart)) {
|
||
return "Email cannot start or end with special characters";
|
||
}
|
||
|
||
if (/\.\./.test(trimmed)) {
|
||
return "Email cannot contain consecutive dots";
|
||
}
|
||
|
||
return "";
|
||
};
|
||
|
||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||
e.preventDefault();
|
||
setTouched(true);
|
||
const error = validateEmail(email);
|
||
setEmailError(error);
|
||
|
||
if (error) {
|
||
toast.error(error);
|
||
return;
|
||
}
|
||
const normalizedEmail = email.trim().toLowerCase();
|
||
try {
|
||
setLoading(true);
|
||
await forgotPassword(normalizedEmail);
|
||
toast.success("Password reset link sent to your email successfully.");
|
||
setTouched(false);
|
||
navigate(all_routes.login);
|
||
} catch (err) {
|
||
toast.error("Failed to send reset link.");
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="login-page-container">
|
||
{/* LEFT SIDE – CARD */}
|
||
<div className="login-right-card">
|
||
<div className="login-wrapper" role="main">
|
||
<form onSubmit={handleSubmit} aria-live="polite">
|
||
{/* LOGO */}
|
||
<div className="text-center mb-4">
|
||
<div className="logo-wrapper">
|
||
|
||
</div>
|
||
</div>
|
||
|
||
{/* TITLE */}
|
||
<div className="text-center mb-4">
|
||
<h4 className="fw-semibold" id="forgot-title">
|
||
Forgot Password?
|
||
</h4>
|
||
<p className="text-muted" id="forgot-desc">
|
||
We’ll send a reset link to your registered email
|
||
</p>
|
||
</div>
|
||
|
||
{/* EMAIL */}
|
||
<div className="mb-3">
|
||
<label htmlFor="email" className="form-label">
|
||
Email Address <span className="text-danger">*</span>
|
||
</label>
|
||
|
||
<div className="input-with-icon">
|
||
<i className="bi bi-envelope"></i>
|
||
<input
|
||
id="email"
|
||
type="email"
|
||
className={`form-control ${emailError ? "is-invalid" : ""}`}
|
||
value={email}
|
||
autoFocus
|
||
aria-label="Email address"
|
||
aria-required="true"
|
||
aria-describedby="forgot-desc"
|
||
onChange={(e) => {
|
||
setEmail(e.target.value);
|
||
if (touched) {
|
||
setEmailError(validateEmail(e.target.value));
|
||
}
|
||
}}
|
||
onBlur={(e) => {
|
||
setTouched(true);
|
||
setEmailError(validateEmail(e.target.value));
|
||
}}
|
||
placeholder="Enter your email address"
|
||
/>
|
||
</div>
|
||
|
||
{emailError && (
|
||
<small className="text-danger">{emailError}</small>
|
||
)}
|
||
</div>
|
||
|
||
{/* SUBMIT */}
|
||
<button
|
||
type="submit"
|
||
className="btn btn-primary w-100 d-flex align-items-center justify-content-center gap-2"
|
||
disabled={loading}
|
||
aria-busy={loading}
|
||
style={{
|
||
position: "relative",
|
||
overflow: "hidden",
|
||
}}
|
||
>
|
||
{loading && (
|
||
<span
|
||
className="spinner-border spinner-border-sm"
|
||
role="status"
|
||
aria-hidden="true"
|
||
style={{ position: "relative", zIndex: 2 }}
|
||
/>
|
||
)}
|
||
<span className="visually-hidden" aria-live="assertive" style={{ position: "relative", zIndex: 2 }}>
|
||
{loading ? "Sending reset link" : ""}
|
||
</span>
|
||
<span style={{ position: "relative", zIndex: 2 }}>
|
||
{loading ? "Sending..." : "Send Reset Link"}
|
||
</span>
|
||
<span
|
||
style={{
|
||
position: "absolute",
|
||
top: 0,
|
||
left: 0,
|
||
width: "100%",
|
||
height: "100%",
|
||
background: "linear-gradient(to right, transparent 0%, rgba(255, 255, 255, 0.4) 50%, transparent 100%)",
|
||
transform: "skewX(-20deg) translateX(-150%)",
|
||
animation: "shine 3s infinite",
|
||
}}
|
||
/>
|
||
</button>
|
||
|
||
{/* BACK TO LOGIN */}
|
||
<div className="text-center mt-4">
|
||
<h6>
|
||
Return to{" "}
|
||
<Link to={all_routes.login} className="text-primary fw-medium">
|
||
Login
|
||
</Link>
|
||
</h6>
|
||
</div>
|
||
|
||
{/* FOOTER */}
|
||
<div className="text-center mt-2">
|
||
<p className="footer-text">© 2025 - CRM</p>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
{/* RIGHT SIDE – STATIC IMAGE */}
|
||
<div className="login-left-image">
|
||
<ImageWithBasePath
|
||
src="assets/img/authentication/reset-img.png"
|
||
className="img-fluid auth-static-img"
|
||
alt="Password reset illustration"
|
||
/>
|
||
</div>
|
||
|
||
{/* STYLES */}
|
||
<style>{`
|
||
.login-page-container {
|
||
display: flex;
|
||
height: 100vh;
|
||
width: 100%;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.login-right-card {
|
||
flex: 1;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
padding: 40px;
|
||
background: linear-gradient(
|
||
135deg,
|
||
#ffeaea 0%,
|
||
#fff5f5 50%,
|
||
#ffdede 100%
|
||
);
|
||
}
|
||
|
||
.login-wrapper {
|
||
width: 100%;
|
||
max-width: 420px;
|
||
background: #ffffff;
|
||
border-radius: 18px;
|
||
padding: 32px;
|
||
box-shadow: 0 18px 45px rgba(15, 23, 42, 0.12);
|
||
animation: fadeInUp 0.8s ease forwards;
|
||
}
|
||
|
||
.logo-wrapper img {
|
||
width: 120px;
|
||
}
|
||
|
||
.login-left-image {
|
||
flex: 1;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
background-color: #f8f9fa;
|
||
animation: fadeInDelayed 1s ease forwards;
|
||
}
|
||
|
||
.auth-static-img {
|
||
width: 100%;
|
||
height: 100%;
|
||
object-fit: contain;
|
||
padding: 40px;
|
||
}
|
||
|
||
.footer-text {
|
||
color: #aaa;
|
||
font-size: 13px;
|
||
}
|
||
|
||
/* Accessible focus ring */
|
||
.form-control:focus,
|
||
.btn:focus {
|
||
outline: none;
|
||
border-color: #8B5CF6;
|
||
box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.25);
|
||
}
|
||
|
||
@media (max-width: 768px) {
|
||
.login-left-image {
|
||
display: none;
|
||
}
|
||
.login-right-card {
|
||
padding: 20px;
|
||
}
|
||
}
|
||
|
||
/* ===== Animation Sync with Login ===== */
|
||
@keyframes fadeInUp {
|
||
from {
|
||
opacity: 0;
|
||
transform: translateY(10px) scale(0.97);
|
||
}
|
||
to {
|
||
opacity: 1;
|
||
transform: translateY(0) scale(1);
|
||
}
|
||
}
|
||
|
||
@keyframes fadeInDelayed {
|
||
from {
|
||
opacity: 0;
|
||
}
|
||
to {
|
||
opacity: 1;
|
||
}
|
||
}
|
||
|
||
.input-with-icon {
|
||
position: relative;
|
||
}
|
||
|
||
.input-with-icon i {
|
||
position: absolute;
|
||
top: 50%;
|
||
left: 12px;
|
||
transform: translateY(-50%);
|
||
font-size: 18px;
|
||
opacity: 0.8;
|
||
}
|
||
|
||
.input-with-icon .form-control {
|
||
padding-left: 40px !important;
|
||
height: 45px;
|
||
border-radius: 8px;
|
||
}
|
||
|
||
|
||
/* Focus Style (Purple Brand Color) */
|
||
.input-with-icon .form-control:focus {
|
||
border-color: #8B5CF6;
|
||
box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.2);
|
||
outline: none;
|
||
}
|
||
`}</style>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default ForgotPassword;
|