File size: 3,657 Bytes
9fbb3f2 | 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 | import React, { useState } from "react";
export default function AuthScreen({ onAuth }) {
const [mode, setMode] = useState("login");
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const submit = async (e) => {
e.preventDefault();
setError("");
if (!username.trim()) {
setError("Username is required.");
return;
}
if (password.length < 4) {
setError("Password must be at least 4 characters.");
return;
}
if (mode === "signup" && password !== confirmPassword) {
setError("Passwords do not match.");
return;
}
setLoading(true);
try {
const endpoint = mode === "login" ? "/auth/login" : "/auth/signup";
const res = await fetch(endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: username.trim(), password }),
});
if (!res.ok) {
const data = await res.json();
throw new Error(data.detail || "Authentication failed.");
}
const data = await res.json();
onAuth(data.token, data.username);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
return (
<div className="auth-backdrop">
<div className="auth-card">
<h1 className="auth-title">Portfolio CIO</h1>
<p className="auth-subtitle">
{mode === "login" ? "Sign in to your account" : "Create a new account"}
</p>
{error && <div className="auth-error">{error}</div>}
<form onSubmit={submit} className="auth-form">
<label>Username</label>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Enter username"
autoFocus
/>
<label>Password</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Enter password"
/>
{mode === "signup" && (
<>
<label>Confirm Password</label>
<input
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
placeholder="Confirm password"
/>
</>
)}
<button type="submit" className="btn primary auth-btn" disabled={loading}>
{loading
? "Please wait..."
: mode === "login"
? "Sign In"
: "Sign Up"}
</button>
</form>
<p className="auth-toggle">
{mode === "login" ? (
<>
Don't have an account?{" "}
<button
className="auth-link"
onClick={() => {
setMode("signup");
setError("");
}}
>
Sign Up
</button>
</>
) : (
<>
Already have an account?{" "}
<button
className="auth-link"
onClick={() => {
setMode("login");
setError("");
}}
>
Sign In
</button>
</>
)}
</p>
</div>
</div>
);
}
|