602 lines
18 KiB
TypeScript
602 lines
18 KiB
TypeScript
import React, { useState, useEffect } from "react";
|
|
import { Link, useNavigate } from "react-router-dom";
|
|
import { all_routes } from "../router/all_routes";
|
|
import {
|
|
checkAuthAccess,
|
|
DecodeToken,
|
|
loginUser,
|
|
showLockCountdownToast,
|
|
} from "../../api/authService";
|
|
import { useDispatch } from "react-redux";
|
|
import { login } from "../../core/data/redux/authSlice";
|
|
import { toast } from "react-toastify";
|
|
import {
|
|
setupAutoLogoutWatcher,
|
|
setupRefreshTokenWatcher,
|
|
} from "../../api/token";
|
|
import { checkRazorpayAccess } from "../../api/razorpay";
|
|
import { startAutoLockWatcher } from "../../api/autolock";
|
|
|
|
const slides = [
|
|
// { img: "/assets/img/loginpage.png", text: "Track Leads Faster" },
|
|
// { img: "/assets/img/loginpage2.jpeg", text: "Sales Pipeline" },
|
|
// { img: "/assets/img/loginpage3.png", text: "Workflow Automation" },
|
|
// { img: "/assets/img/loginpage4.png", text: "Multi-User CRM" },
|
|
// { img: "/assets/img/loginpage5.png", text: "Analytics & Reporting" },
|
|
{ img: "/assets/img/authentication/email-img.png", text: "Analytics & Reporting" },
|
|
{ img: "/assets/img/authentication/login-img.png", text: "Analytics & Reporting" },
|
|
{ img: "/assets/img/authentication/register-img.png", text: "Analytics & Reporting" },
|
|
{ img: "/assets/img/authentication/success-img.png", text: "Analytics & Reporting" },
|
|
{ img: "/assets/img/authentication/verify-img.png", text: "Analytics & Reporting" },
|
|
];
|
|
|
|
const LeftImage = () => {
|
|
const [randomImage, setRandomImage] = useState(slides[0].img);
|
|
|
|
useEffect(() => {
|
|
const rand = Math.floor(Math.random() * slides.length);
|
|
setRandomImage(slides[rand].img);
|
|
}, []);
|
|
|
|
return (
|
|
<div className="fixed-left-image">
|
|
<img src={randomImage} alt="Login Visual" />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const Login: React.FC = () => {
|
|
const navigate = useNavigate();
|
|
const dispatch = useDispatch();
|
|
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [rememberMe, setRememberMe] = useState(false);
|
|
const [isPasswordVisible, setPasswordVisible] = useState(false);
|
|
|
|
const [emailError, setEmailError] = useState("");
|
|
const [passwordError, setPasswordError] = useState("");
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
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);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
setLoading(false);
|
|
setEmail("");
|
|
setPassword("");
|
|
setEmailError("");
|
|
setPasswordError("");
|
|
setRememberMe(false);
|
|
}, []);
|
|
|
|
|
|
useEffect(() => {
|
|
const token = localStorage.getItem("authToken");
|
|
const role = localStorage.getItem("role");
|
|
|
|
if (window.location.pathname === "/login" && token && role) {
|
|
if (role === "superadmin") {
|
|
navigate(all_routes.superadminDashboard, { replace: true });
|
|
} else if (role === "client") {
|
|
navigate(all_routes.manageusers, { replace: true });
|
|
} else if (role === "admin") {
|
|
navigate(all_routes.dealsDashboard, { replace: true });
|
|
} else {
|
|
navigate(all_routes.leadsDashboard, { replace: true });
|
|
}
|
|
}
|
|
}, [navigate]);
|
|
|
|
|
|
// EMAIL VALIDATION - Live on every change
|
|
const validateEmail = (value: string) => {
|
|
const trimmed = value.trim();
|
|
|
|
if (!trimmed) return "Email is required";
|
|
|
|
if (/[A-Z]/.test(trimmed)) {
|
|
return "Email must be in lowercase only";
|
|
}
|
|
if (trimmed.length > 100) return "Email must not exceed 100 characters";
|
|
if (/\s{2,}/.test(value)) return "Email cannot contain consecutive spaces";
|
|
if (/<[^>]*>/g.test(value))
|
|
return "Email cannot contain HTML or script tags";
|
|
|
|
const allowedCharsRegex = /^[A-Za-z0-9@$&\/\-_. ,]+$/;
|
|
if (!allowedCharsRegex.test(value))
|
|
return "Email contains invalid special characters";
|
|
|
|
if (/^\d+$/.test(trimmed)) return "Email cannot contain only numbers";
|
|
if (/^[^A-Za-z0-9]+$/.test(trimmed))
|
|
return "Email cannot contain only symbols";
|
|
if (/(.)\1{4,}/.test(trimmed))
|
|
return "Email contains too many repeated characters";
|
|
|
|
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 (domain && 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 "";
|
|
};
|
|
|
|
// PASSWORD VALIDATION - Live on every change
|
|
const validatePassword = (value: string) => {
|
|
if (!value) return "Password is required";
|
|
if (value.length < 8) return "Password must be at least 8 characters";
|
|
if (value.length > 50) return "Password must not exceed 50 characters";
|
|
if (!/[A-Z]/.test(value))
|
|
return "Password must contain at least one uppercase letter";
|
|
if (!/[a-z]/.test(value))
|
|
return "Password must contain at least one lowercase letter";
|
|
if (!/\d/.test(value)) return "Password must contain at least one number";
|
|
if (!/[@$!%*?&]/.test(value))
|
|
return "Password must contain at least one special character (@$!%*?&)";
|
|
if (/\s{2,}/.test(value))
|
|
return "Password cannot contain consecutive spaces";
|
|
if (/(.)\1{5,}/.test(value))
|
|
return "Password contains too many repeated characters";
|
|
|
|
return "";
|
|
};
|
|
|
|
const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const value = e.target.value;
|
|
setEmail(value);
|
|
setEmailError(validateEmail(value)); // Live validation
|
|
};
|
|
|
|
const handlePasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const value = e.target.value;
|
|
setPassword(value);
|
|
setPasswordError(validatePassword(value)); // Live validation
|
|
};
|
|
|
|
const handleLogin = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
|
|
const emailErr = validateEmail(email);
|
|
const passErr = validatePassword(password);
|
|
|
|
setEmailError(emailErr);
|
|
setPasswordError(passErr);
|
|
|
|
if (emailErr || passErr) {
|
|
if (emailErr) toast.error(emailErr);
|
|
if (passErr) toast.error(passErr);
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
try {
|
|
const data = await loginUser(
|
|
email.trim().toLowerCase(),
|
|
password,
|
|
rememberMe
|
|
);
|
|
|
|
if (!data.access_token) {
|
|
toast.error("Invalid credentials");
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
// FIXED: Store full "Bearer xxx" token
|
|
const fullToken = `Bearer ${data.access_token}`;
|
|
localStorage.setItem("authToken", fullToken);
|
|
|
|
//const userInfo = await DecodeToken();
|
|
const userInfo = await checkAuthAccess();
|
|
// Save user info
|
|
Object.entries(userInfo).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null) {
|
|
localStorage.setItem(key, String(value));
|
|
}
|
|
});
|
|
Object.entries(userInfo).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null) {
|
|
localStorage.setItem(key, String(value));
|
|
}
|
|
});
|
|
if (rememberMe) {
|
|
localStorage.setItem("remember_me", "true");
|
|
} else {
|
|
localStorage.removeItem("remember_me");
|
|
}
|
|
|
|
dispatch(
|
|
login({
|
|
token: fullToken,
|
|
refreshToken: data.refresh_token ?? "",
|
|
role: userInfo.role,
|
|
client_id: userInfo.client_id,
|
|
user_id: userInfo.user_id,
|
|
e_mail: userInfo.email,
|
|
username: userInfo.username,
|
|
})
|
|
);
|
|
|
|
|
|
//setupRefreshTokenWatcher();
|
|
|
|
// Set up auto logout watcher after login
|
|
if (userInfo.expires_at) {
|
|
setupAutoLogoutWatcher();
|
|
}
|
|
|
|
const userRole = userInfo.role;
|
|
// Superadmin → always allowed
|
|
if (userRole === "superadmin") {
|
|
navigate(all_routes.superadminDashboard,{ replace: true });
|
|
return;
|
|
}
|
|
|
|
// Client → special handling
|
|
if (userRole === "client") {
|
|
if (userInfo.access) {
|
|
navigate(all_routes.manageusers,{ replace: true });
|
|
} else {
|
|
navigate(all_routes.payments,{ replace: true });
|
|
}
|
|
return;
|
|
}
|
|
if (!userRole) {
|
|
toast.error("Invalid session. Please login again.");
|
|
return;
|
|
}
|
|
|
|
// 🔒 Admin & others → must have access
|
|
if (!userInfo.access) {
|
|
toast.error("You don't have an active subscription, Contact admin.");
|
|
return; // ⛔ stay on login
|
|
}
|
|
toast.success("Login successfully");
|
|
// ✅ Access granted
|
|
if (userRole === "admin") {
|
|
navigate(all_routes.dealsDashboard, { replace: true });
|
|
} else {
|
|
navigate(all_routes.leadsDashboard, { replace: true });
|
|
}
|
|
|
|
|
|
startAutoLockWatcher(navigate);
|
|
} catch (err: any) {
|
|
const message = err.message || "Login failed";
|
|
|
|
const match = message.match(/after\s+(\d+)\s*minute/i);
|
|
if (match) {
|
|
const minutes = parseInt(match[1], 10);
|
|
showLockCountdownToast(minutes);
|
|
return;
|
|
}
|
|
// ⚠️ Password reset required → WARNING
|
|
if (err?.type === "warning") {
|
|
toast.warning(message, {
|
|
autoClose: 6000,
|
|
});
|
|
return;
|
|
}
|
|
// ❌ Default error
|
|
toast.error(message);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="login-page-container">
|
|
{/* LEFT SIDE IMAGE */}
|
|
<div className="login-left-image">
|
|
<LeftImage />
|
|
</div>
|
|
|
|
{/* RIGHT SIDE CARD */}
|
|
<div className="login-right-card">
|
|
<div className="login-wrapper">
|
|
<form onSubmit={handleLogin}>
|
|
{/* LOGO */}
|
|
<div className="text-center mb-4">
|
|
|
|
</div>
|
|
|
|
{/* TITLE */}
|
|
<h2 className="text-center fw-bold" style={{ color: "#8B5CF6" }}>
|
|
Login to CRM
|
|
</h2>
|
|
<p className="text-center mb-4" style={{ color: "#64748B" }}>
|
|
Welcome back! Please sign in
|
|
</p>
|
|
|
|
{/* 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}
|
|
onChange={handleEmailChange}
|
|
placeholder="Enter your email address"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
{emailError && (
|
|
<small className="text-danger d-block mt-1">{emailError}</small>
|
|
)}
|
|
</div>
|
|
|
|
{/* PASSWORD */}
|
|
<div className="mb-3">
|
|
<label htmlFor="password" className="form-label">
|
|
Password <span className="text-danger">*</span>
|
|
</label>
|
|
<div className="input-with-icon">
|
|
<i className="bi bi-lock"></i>
|
|
<input
|
|
id="password"
|
|
type={isPasswordVisible ? "text" : "password"}
|
|
className={`form-control ${
|
|
passwordError ? "is-invalid" : ""
|
|
}`}
|
|
value={password}
|
|
onChange={handlePasswordChange}
|
|
placeholder="Enter your password"
|
|
style={{ paddingRight: "45px" }}
|
|
/>
|
|
<span
|
|
onClick={() => setPasswordVisible((prev) => !prev)}
|
|
className={`toggle-eye bi ${
|
|
isPasswordVisible ? "bi-eye" : "bi-eye-slash"
|
|
}`}
|
|
style={{ pointerEvents: "auto", zIndex: 10 }}
|
|
/>
|
|
</div>
|
|
{passwordError && (
|
|
<small className="text-danger d-block mt-1">
|
|
{passwordError}
|
|
</small>
|
|
)}
|
|
</div>
|
|
|
|
{/* OPTIONS */}
|
|
<div className="d-flex justify-content-between align-items-center mb-3">
|
|
<div className="form-check">
|
|
<input
|
|
type="checkbox"
|
|
className="form-check-input"
|
|
id="rememberMe"
|
|
checked={rememberMe}
|
|
onChange={(e) => setRememberMe(e.target.checked)}
|
|
/>
|
|
<label className="form-check-label" htmlFor="rememberMe">
|
|
Remember me
|
|
</label>
|
|
</div>
|
|
<Link
|
|
className="text-primary fw-medium"
|
|
to={all_routes.forgotPassword}
|
|
>
|
|
Forgot Password?
|
|
</Link>
|
|
</div>
|
|
|
|
{/* SUBMIT */}
|
|
<button
|
|
type="submit"
|
|
className="btn btn-primary w-100 d-flex align-items-center justify-content-center gap-2"
|
|
disabled={loading}
|
|
style={{
|
|
background: "linear-gradient(135deg, #8B5CF6 0%, #7C3AED 100%)",
|
|
borderColor: "#7C3AED",
|
|
padding: "10px",
|
|
borderRadius: "8px",
|
|
fontWeight: "bold",
|
|
position: "relative",
|
|
overflow: "hidden",
|
|
boxShadow: "0 4px 14px rgba(139, 92, 246, 0.35)",
|
|
}}
|
|
>
|
|
{loading && (
|
|
<span
|
|
className="spinner-border spinner-border-sm"
|
|
role="status"
|
|
aria-hidden="true"
|
|
style={{ position: "relative", zIndex: 2 }}
|
|
></span>
|
|
)}
|
|
<span style={{ position: "relative", zIndex: 2 }}>
|
|
{loading ? "Signing In..." : "Next"}
|
|
</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>
|
|
|
|
{/* DIVIDER */}
|
|
<div className="login-divider">or</div>
|
|
|
|
{/* CONTACT US */}
|
|
<p style={{ textAlign: "center", color: "#666" }}>
|
|
<span>Don't have an account? </span>
|
|
<a
|
|
href="https://www.iattechnologies.com"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
style={{ color: "#8B5CF6", fontWeight: 600, cursor: "pointer" }}
|
|
>
|
|
Contact Us
|
|
</a>
|
|
</p>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
{/* STYLES - Fixed layout collapse */}
|
|
<style>{`
|
|
@media (min-width: 768px) {
|
|
.login-wrapper img { width: 150px !important; }
|
|
}
|
|
|
|
.login-page-container {
|
|
display: flex;
|
|
height: 100vh;
|
|
width: 100%;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.login-left-image {
|
|
flex: 1;
|
|
background-color: #f3f3f3;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
overflow: hidden;
|
|
animation: fadeInDelayed 1s ease forwards;
|
|
}
|
|
|
|
.login-left-image img {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: contain;
|
|
}
|
|
|
|
.login-right-card {
|
|
flex: 1;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 40px;
|
|
background: linear-gradient(135deg, #F8FAFC 0%, #F3E8FF 50%, #E0F2FE 100%);
|
|
box-shadow: inset 0px 0px 120px rgba(139, 92, 246, 0.12);
|
|
}
|
|
|
|
.login-wrapper {
|
|
max-width: 420px;
|
|
width: 100%;
|
|
background-color: #ffffff;
|
|
border-radius: 18px;
|
|
box-shadow: 0 18px 45px rgba(139, 92, 246, 0.12);
|
|
padding: 28px 32px;
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.login-left-image { display: none; }
|
|
.login-right-card { padding: 20px; }
|
|
}
|
|
|
|
.input-with-icon {
|
|
position: relative;
|
|
}
|
|
|
|
.input-with-icon i {
|
|
position: absolute;
|
|
top: 50%;
|
|
left: 12px;
|
|
transform: translateY(-50%);
|
|
font-size: 18px;
|
|
opacity: 0.8;
|
|
z-index: 2;
|
|
pointer-events: none;
|
|
}
|
|
|
|
.input-with-icon .form-control {
|
|
padding-left: 40px !important;
|
|
padding-right: 45px !important; /* Space for eye icon */
|
|
height: 45px;
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.input-with-icon .form-control:focus {
|
|
border-color: #8B5CF6;
|
|
box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.2);
|
|
outline: none;
|
|
}
|
|
.input-with-icon .form-control.is-invalid {
|
|
padding-right: 45px !important; /* Ensure same padding even when invalid */
|
|
}
|
|
.input-with-icon .form-control.is-invalid {
|
|
padding-right: 45px !important;
|
|
background-image: none; /* Optional: removes default bootstrap exclamation mark */
|
|
}
|
|
.toggle-eye {
|
|
position: absolute;
|
|
top: 50%;
|
|
right: 12px;
|
|
transform: translateY(-50%);
|
|
font-size: 20px;
|
|
cursor: pointer;
|
|
color: #8B5CF6;
|
|
z-index: 2;
|
|
}
|
|
|
|
.login-divider {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: #777;
|
|
margin: 20px 0;
|
|
position: relative;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.login-divider::before,
|
|
.login-divider::after {
|
|
content: "";
|
|
flex: 1;
|
|
height: 1px;
|
|
background: #ddd;
|
|
margin: 0 10px;
|
|
}
|
|
`}</style>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Login;
|