feat: added login page

This commit is contained in:
nihonium 2025-11-23 03:57:35 +03:00
parent 69e8a8dc79
commit c500116916
Signed by: nihonium
GPG key ID: 0251623741027CFC
12 changed files with 730 additions and 1 deletions

View file

@ -0,0 +1,116 @@
import React, { useState } from "react";
import { AuthService } from "../../auth/services/AuthService";
import { useNavigate } from "react-router-dom";
export const LoginPage: React.FC = () => {
const navigate = useNavigate();
const [isLogin, setIsLogin] = useState(true); // true = login, false = signup
const [nickname, setNickname] = useState("");
const [password, setPassword] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError(null);
try {
if (isLogin) {
const res = await AuthService.postAuthSignIn({ nickname, pass: password });
if (res.success) {
// TODO: сохранить JWT в localStorage/cookie
console.log("Logged in user id:", res.user_id);
navigate("/"); // редирект после успешного входа
} else {
setError(res.error || "Login failed");
}
} else {
const res = await AuthService.postAuthSignUp({ nickname, pass: password });
if (res.success) {
console.log("User signed up with id:", res.user_id);
setIsLogin(true); // переключаемся на login после регистрации
} else {
setError(res.error || "Sign up failed");
}
}
} catch (err: any) {
console.error(err);
setError(err?.message || "Something went wrong");
} finally {
setLoading(false);
}
};
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50 px-4">
<div className="max-w-md w-full bg-white shadow-md rounded-lg p-8">
<h2 className="text-2xl font-bold mb-6 text-center">
{isLogin ? "Login" : "Sign Up"}
</h2>
{error && <div className="text-red-600 mb-4">{error}</div>}
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Nickname
</label>
<input
type="text"
value={nickname}
onChange={(e) => setNickname(e.target.value)}
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Password
</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
required
/>
</div>
<button
type="submit"
disabled={loading}
className="w-full bg-blue-600 text-white py-2 rounded-lg font-semibold hover:bg-blue-700 transition disabled:opacity-50"
>
{loading ? "Please wait..." : isLogin ? "Login" : "Sign Up"}
</button>
</form>
<div className="mt-4 text-center text-sm text-gray-600">
{isLogin ? (
<>
Don't have an account?{" "}
<button
onClick={() => setIsLogin(false)}
className="text-blue-600 hover:underline"
>
Sign Up
</button>
</>
) : (
<>
Already have an account?{" "}
<button
onClick={() => setIsLogin(true)}
className="text-blue-600 hover:underline"
>
Login
</button>
</>
)}
</div>
</div>
</div>
);
};