Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | 3x 3x 3x 3x 3x 34x 33x 3x 82x 82x 82x 82x 82x 82x 82x 82x 82x 82x 23x 1x 82x 18x 18x 82x 16x 16x 82x 16x 16x 16x 4x 4x 12x 12x 12x 3x 3x 3x 9x 9x 9x 6x 6x 6x 16x 6x 3x 3x 9x 82x 18x 16x | import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import axios from "axios";
import { REQUEST_TIMEOUT_MS } from "@/utils/axios/axiosInterceptor";
import CircularProgress from "@mui/material/CircularProgress";
import ErrorOutlineIcon from "@mui/icons-material/ErrorOutline";
import Visibility from "@mui/icons-material/Visibility";
import VisibilityOff from "@mui/icons-material/VisibilityOff";
import MVP_LOGO from "@/assets/logo/mvp_logo2.png";
import { API_ENDPOINTS } from "@/api/endpoints";
import { useAuthSession } from "@/authentication/AuthSessionProvider";
import { setStoredRole, setCsrfToken } from "@/authentication/session";
import "./LoginPage.scss";
// Dedicated instance without global axios interceptors — avoids the 401→redirect
// handler firing on wrong-password responses during the login flow itself.
// withCredentials so the backend's Set-Cookie (httpOnly access_token) is stored.
// Explicit timeout rather than relying on axios.defaults: this client is created
// at MODULE scope, so whether it inherits the default depends on whether this
// module is imported before or after setupAxiosInterceptors() runs. Login is the
// one request a user cannot work around by navigating elsewhere — it must never
// hang silently. See REQUEST_TIMEOUT_MS.
const loginClient = axios.create({ withCredentials: true, timeout: REQUEST_TIMEOUT_MS });
// EI-2962: login-page field length caps. Email ≤ 50, password ≤ 30. Kept as
// named constants so the messages and the tests reference one source of truth.
export const EMAIL_MAX_LENGTH = 50;
export const PASSWORD_MAX_LENGTH = 30;
export const EMAIL_TOO_LONG_MESSAGE = `Email must be ${EMAIL_MAX_LENGTH} characters or fewer.`;
export const PASSWORD_TOO_LONG_MESSAGE = `Password must be ${PASSWORD_MAX_LENGTH} characters or fewer.`;
export const validateEmailLength = (value) => (value.length > EMAIL_MAX_LENGTH ? EMAIL_TOO_LONG_MESSAGE : "");
export const validatePasswordLength = (value) => (value.length > PASSWORD_MAX_LENGTH ? PASSWORD_TOO_LONG_MESSAGE : "");
export const LoginPage = () => {
const navigate = useNavigate();
const { loading, isAuthenticated, role: sessionRole, refresh } = useAuthSession();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [showPassword, setShowPassword] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState(null);
const [emailError, setEmailError] = useState("");
const [passwordError, setPasswordError] = useState("");
// Already-authenticated guard: if the /auth/me probe says we have a valid
// session cookie, skip the form and route to the portal.
useEffect(() => {
if (!loading && isAuthenticated) {
navigate(sessionRole ? `/${sessionRole}` : "/", { replace: true });
}
}, [loading, isAuthenticated, sessionRole, navigate]);
const handleEmailChange = (value) => {
setEmail(value);
setEmailError(validateEmailLength(value));
};
const handlePasswordChange = (value) => {
setPassword(value);
setPasswordError(validatePasswordLength(value));
};
const handleSubmit = async (e) => {
e.preventDefault();
setError(null);
if (!email.trim() || !password) {
setError("Please enter your email and password.");
return;
}
// EI-2962: block submit when either field exceeds its length cap and
// surface the inline per-field validation message.
const emailLengthError = validateEmailLength(email);
const passwordLengthError = validatePasswordLength(password);
if (emailLengthError || passwordLengthError) {
setEmailError(emailLengthError);
setPasswordError(passwordLengthError);
return;
}
setIsSubmitting(true);
try {
const response = await loginClient.post(API_ENDPOINTS.auth.login, {
email: email.trim().toLowerCase(),
password,
});
Eif (response.data?.csrf_token) setCsrfToken(response.data.csrf_token);
// BFF (ADR-001 Phase 2): the access token came back as an httpOnly
// cookie — we never touch it. Re-probe the session (/auth/me) so the
// guards see the new state, and resolve the role from the login body
// (fast path) or the session probe (fallback if the body omits it).
const session = await refresh();
const role = response.data?.role || session.role || null;
if (role) setStoredRole(role);
navigate(role ? `/${role}` : "/", { replace: true });
} catch (err) {
const detail = err?.response?.data?.detail;
setError(detail || "Login failed. Please check your credentials and try again.");
} finally {
setIsSubmitting(false);
}
};
return (
<div id="login-page" className="login-page">
<div className="login-background">
<div className="gradient-overlay" />
</div>
<div id="login-container" className="login-container">
<div id="login-logo-section" className="login-logo-section">
<img id="login-logo" src={MVP_LOGO} alt="EruditionTX" className="login-logo" />
<span id="login-logo-text" className="login-logo-text">
EruditionTX
</span>
</div>
<div id="login-box" className="login-card">
<div id="login-header" className="login-header">
<h1 id="login-title" className="login-title">
Welcome Back
</h1>
<p id="login-subtitle" className="login-subtitle">
Sign in to your account
</p>
</div>
{error && (
<div id="error-message" className="login-error">
<ErrorOutlineIcon sx={{ fontSize: 18, flexShrink: 0, marginTop: "1px" }} />
<span>{error}</span>
</div>
)}
<form id="login-form" className="login-form" onSubmit={handleSubmit} noValidate>
<div id="email-field" className="form-field">
<label htmlFor="email">Email address</label>
<div id="email-input-wrapper" className="input-wrapper">
<input
id="email"
type="email"
placeholder="you@example.com"
value={email}
onChange={(e) => handleEmailChange(e.target.value)}
autoComplete="email"
aria-invalid={Boolean(emailError)}
aria-describedby={emailError ? "email-error" : undefined}
required
disabled={isSubmitting}
/>
</div>
{emailError && (
<span id="email-error" className="field-error" data-testid="email-error" role="alert">
{emailError}
</span>
)}
</div>
<div id="password-field" className="form-field">
<label htmlFor="password">Password</label>
<div id="password-input-wrapper" className="input-wrapper">
<input
id="password"
type={showPassword ? "text" : "password"}
placeholder="Your password"
value={password}
onChange={(e) => handlePasswordChange(e.target.value)}
autoComplete="current-password"
aria-invalid={Boolean(passwordError)}
aria-describedby={passwordError ? "password-error" : undefined}
required
disabled={isSubmitting}
className="input-password"
/>
<button
id="btn-toggle-password"
type="button"
className="toggle-password"
onClick={() => setShowPassword((s) => !s)}
aria-label="toggle password visibility"
tabIndex={-1}
>
{showPassword ? (
<VisibilityOff sx={{ fontSize: 18 }} />
) : (
<Visibility sx={{ fontSize: 18 }} />
)}
</button>
</div>
{passwordError && (
<span
id="password-error"
className="field-error"
data-testid="password-error"
role="alert"
>
{passwordError}
</span>
)}
</div>
<div id="forgot-password-wrapper" className="forgot-link">
<a
id="link-forgot-password"
href="/forgot-password"
onClick={(e) => {
e.preventDefault();
navigate("/forgot-password");
}}
>
Forgot password?
</a>
</div>
<button id="btn-login" type="submit" className="login-submit" disabled={isSubmitting}>
{isSubmitting ? <CircularProgress size={20} sx={{ color: "#003754" }} /> : "Log In"}
</button>
</form>
<div id="login-divider" className="login-divider">
<span>or</span>
</div>
<div id="login-signup-prompt" className="login-footer">
<a
id="link-back-home"
href="/"
onClick={(e) => {
e.preventDefault();
navigate("/");
}}
>
Back to home
</a>
</div>
</div>
</div>
</div>
);
};
export default LoginPage;
|