Files
frontend/app/AuthPage.jsx
fullofempt e4bfbd30cc end
2025-12-15 13:18:18 +05:00

163 lines
6.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import React, { useState } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "../app/context/AuthContext";
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const AuthPage = () => {
const router = useRouter();
const { login } = useAuth();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [rememberMe, setRememberMe] = useState(false);
const [checkboxError, setCheckboxError] = useState(false);
const [authError, setAuthError] = useState("");
const [isSubmitting, setIsSubmitting] = useState(false);
const isEmailValid = emailRegex.test(email);
const isFormValid = isEmailValid && password.length > 0;
const handleSubmit = async (e) => {
e.preventDefault();
if (!rememberMe) {
setCheckboxError(true);
return;
}
setCheckboxError(false);
if (!isFormValid) return;
try {
setAuthError("");
setIsSubmitting(true);
await login(email, password); // теперь это async
} catch (err) {
setAuthError(err.message || "Неверный логин или пароль");
setIsSubmitting(false);
}
};
return (
<div className="min-h-screen w-full bg-[#90D2F9] flex items-center justify-center px-4">
<div className="w-full max-w-md bg-white/10 rounded-2xl p-6 sm:p-8 shadow-lg relative">
{/* Красный баннер ошибок */}
{(checkboxError || authError) && (
<div
className="absolute -top-10 left-0 w-full bg-red-500 text-white text-xs sm:text-sm font-montserrat px-3 py-2 rounded-t-2xl flex items-center justify-center shadow-md"
role="alert"
>
{checkboxError
? "Вы не согласны с условиями использования"
: authError || "Неверный логин или пароль"}
</div>
)}
<h1 className="font-montserrat text-white font-extrabold text-2xl text-center">
Авторизация
</h1>
<form onSubmit={handleSubmit} className="mt-6 space-y-4">
{/* Почта */}
<div className="space-y-1">
<label className="block font-montserrat font-extrabold text-xs text-[#ffffff]">
Почта
</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="w-full rounded-full bg-white px-4 py-2 text-sm font-montserrat text-black outline-none focus:ring-2 focus:ring-blue-200"
/>
{!isEmailValid && email.length > 0 && (
<p className="text-[11px] text-red-600 font-montserrat">
Введите корректный email
</p>
)}
</div>
{/* Пароль */}
<div className="space-y-1">
<label className="block font-montserrat font-extrabold text-xs text-[#ffffff]">
Пароль
</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full rounded-full bg-white px-4 py-2 text-sm font-montserrat text-black outline-none focus:ring-2 focus:ring-blue-200"
/>
</div>
{/* Чекбокс */}
<div className="flex items-start space-x-2">
<button
type="button"
onClick={() => {
setRememberMe((prev) => !prev);
if (!rememberMe) setCheckboxError(false);
}}
className={`w-10 h-6 rounded-full border border-white flex items-center justify-center ${
rememberMe ? "bg-white" : "bg-transparent"
}`}
>
{rememberMe && (
<span className="h-2 w-2 rounded-full bg-[#90D2F9]" />
)}
</button>
<p className="font-montserrat font-black text-[12px] leading-[12px] text-red-600">
Подтверждаю, что я прочитал условия использования данного
приложения
</p>
</div>
{/* Ссылки */}
<div className="flex items-center justify-center justify-between text-[15px] font-montserrat font-bold text-[#d60404d8] mt-5">
{/* <button
type="button"
className="hover:underline"
onClick={() => router.push("/recPassword")}
>
Забыли пароль?
</button> */}
<button
type="button"
className="hover:underline"
onClick={() => router.push("/reg")}
>
Регистрация
</button>
</div>
{/* Кнопка Войти */}
<button
type="submit"
disabled={!isFormValid || isSubmitting}
className={` w-full rounded-full py-2 text-center font-montserrat font-extrabold text-sm transition-colors
${
isFormValid && !isSubmitting
? "bg-green-500 text-white hover:bg-green-600"
: "bg-white text-[#C4C4C4] cursor-not-allowed"
}`}
>
{isSubmitting ? "Входим..." : "Войти"}
</button>
</form>
{/* Подсказка по тестовым логинам — можно убрать, когда перейдёшь на реальные аккаунты */}
<div className="mt-4 text-[15px] text-white font-montserrat space-y-1">
<p>Тестовые аккаунты (если настроены на бэке):</p>
<p>Пользователь: user@mail.com / user123123</p>
<p>Волонтёр: vol@mail.com / vol123123</p>
<p>Модератор: mod@mail.com / mod123123</p>
</div>
</div>
</div>
);
};
export default AuthPage;