Spaces:
Build error
Build error
File size: 7,772 Bytes
d571830 | 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 | import { useEffect, useState } from "react";
import { api } from "../api.js";
import { setSession } from "../utils/auth.js";
const SQLI_RE = /(\b(select|insert|update|delete|drop|union|exec|cast|convert|declare|xp_|char|nchar|varchar|alter|create|truncate|sleep|benchmark|waitfor|information_schema)\b|--|;|\/\*|\*\/|0x[0-9a-fA-F]+|\bor\b\s+.{0,30}[=<>]|\band\b\s+.{0,30}[=<>])/i;
function hasSQLI(val) { return SQLI_RE.test(val); }
function getPasswordStrength(pw) {
const score = [/.{8,}/, /[A-Z]/, /[a-z]/, /[0-9]/, /[^A-Za-z0-9]/].filter(r => r.test(pw)).length;
const widths = ['0%', '25%', '45%', '70%', '100%'];
const colors = ['#ef4444', '#f97316', '#eab308', '#22c55e', '#7c3aed'];
return { width: widths[score] || '0%', color: colors[score - 1] || '#334155' };
}
export default function AuthModal({ open, onClose, onAuthed }) {
const [mode, setMode] = useState("login"); // "login" | "register"
const [identifier, setIdentifier] = useState("");
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [busy, setBusy] = useState(false);
const [pwStrength, setPwStrength] = useState({ width: '0%', color: '#334155' });
// Reset state + close on Escape.
useEffect(() => {
if (!open) return;
setError("");
setBusy(false);
setPassword("");
const onKey = (e) => e.key === "Escape" && onClose();
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [open, onClose]);
if (!open) return null;
async function handleSubmit(e) {
e.preventDefault();
setError("");
if (mode === "login") {
if (!identifier.trim() || !password) {
setError("Enter your username/email and password.");
return;
}
if (hasSQLI(identifier) || hasSQLI(password)) {
setError("Invalid input detected.");
return;
}
} else {
if (username.trim().length < 3) {
setError("Username must be at least 3 characters.");
return;
}
if (username.trim().length > 15) {
setError("Username must be 15 characters or less.");
return;
}
if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email.trim())) {
setError("Enter a valid email address.");
return;
}
if (email.trim().length > 20) {
setError("Email must be 20 characters or less.");
return;
}
if (password.length < 6) {
setError("Password must be at least 6 characters.");
return;
}
if (hasSQLI(username) || hasSQLI(email) || hasSQLI(password)) {
setError("Invalid input detected.");
return;
}
}
setBusy(true);
try {
const res =
mode === "login"
? await api.authLogin(identifier.trim(), password)
: await api.authRegister(username.trim(), email.trim(), password);
setSession(res.token, res.user);
onAuthed?.(res.user);
onClose();
} catch (err) {
setError(err.message || "Something went wrong. Try again.");
} finally {
setBusy(false);
}
}
return (
<div className="auth-overlay" onClick={onClose}>
<div className="auth-modal" onClick={(e) => e.stopPropagation()} role="dialog" aria-modal="true">
<button className="auth-modal-close" onClick={onClose} aria-label="Close">
✕
</button>
<div className="auth-modal-logo"><span className="auth-modal-logo-text">ANICOVE</span></div>
<div className="auth-tabs">
<button
className={`auth-tab${mode === "login" ? " active" : ""}`}
onClick={() => { setMode("login"); setError(""); }}
>
Sign in
</button>
<button
className={`auth-tab${mode === "register" ? " active" : ""}`}
onClick={() => { setMode("register"); setError(""); }}
>
Create account
</button>
</div>
<form className="auth-form" onSubmit={handleSubmit}>
{mode === "login" ? (
<label className="auth-field">
<span>Username or email</span>
<input
type="text"
value={identifier}
onChange={(e) => setIdentifier(e.target.value)}
placeholder="username@example.com"
autoComplete="username"
autoFocus
/>
</label>
) : (
<>
<label className="auth-field">
<span>Username</span>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Your username"
autoComplete="username"
autoFocus
/>
</label>
<label className="auth-field">
<span>Email</span>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="you@example.com"
autoComplete="email"
/>
</label>
</>
)}
<label className="auth-field">
<span>Password {mode === "register" && <span style={{color:'#475569',fontWeight:400,textTransform:'none',letterSpacing:0}}>(min 6 characters)</span>}</span>
<input
type="password"
value={password}
onChange={(e) => {
setPassword(e.target.value);
if (mode === "register") setPwStrength(getPasswordStrength(e.target.value));
}}
placeholder="••••••••"
autoComplete={mode === "login" ? "current-password" : "new-password"}
maxLength={18}
/>
{mode === "register" && (
<div className="auth-pw-bar"><div className="auth-pw-fill" style={{width: pwStrength.width, background: pwStrength.color}}></div></div>
)}
</label>
{error ? <div className="auth-error show">{error}</div> : null}
<button type="submit" className="btn btn-primary auth-submit" disabled={busy}>
<span className="auth-submit-text">{busy ? "Please wait…" : mode === "login" ? "Sign in" : "Create account"}</span>
<span className="auth-submit-spinner"></span>
</button>
</form>
<div className="auth-divider"><span>OR</span></div>
<button
className="auth-anilist-btn"
onClick={() => { window.location.href = "/api/auth/anilist/login"; }}
title="Sign in with your AniList account"
>
<img src="https://anilist.co/img/icons/icon.svg" style={{width:20,height:20}} alt="AniList" />
<span>Continue with AniList</span>
</button>
{mode === "login" ? (
<p className="auth-switch">No account? <a onClick={() => { setMode("register"); setError(""); }}>Create one</a></p>
) : (
<p className="auth-switch">Already have an account? <a onClick={() => { setMode("login"); setError(""); }}>Sign in</a></p>
)}
<div className="auth-recaptcha-terms">
This site is protected by reCAPTCHA and the Google
<a href="https://policies.google.com/privacy" target="_blank" rel="noopener noreferrer">Privacy Policy</a> and
<a href="https://policies.google.com/terms" target="_blank" rel="noopener noreferrer">Terms of Service</a> apply.
</div>
</div>
</div>
);
}
|