Страница авторизации
Создана страница-модуль авторизации
This commit is contained in:
parent
8bda3252cd
commit
004980a2cf
@ -1,50 +1,25 @@
|
||||
import { useState } from "react";
|
||||
import { Link, useNavigate } from "react-router-dom";
|
||||
import { useAuth } from "../contexts/AuthContext";
|
||||
import React, { useState } from 'react';
|
||||
import { useNavigate, Link } from 'react-router-dom';
|
||||
import { useAuth } from '../contexts/AuthContext';
|
||||
|
||||
function LoginPage() {
|
||||
const [formData, setFormData] = useState({
|
||||
email: "",
|
||||
password: "",
|
||||
});
|
||||
const [error, setError] = useState("");
|
||||
const LoginPage = () => {
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const { login } = useAuth();
|
||||
const [error, setError] = useState(null);
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleChange = (e) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
[name]: value,
|
||||
}));
|
||||
};
|
||||
const { signIn } = useAuth();
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!formData.email || !formData.password) {
|
||||
setError("Заполните все поля");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setError("");
|
||||
setLoading(true);
|
||||
|
||||
const { error } = await login(formData.email, formData.password);
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
navigate("/", { replace: true });
|
||||
setError(null);
|
||||
await signIn(email, password);
|
||||
navigate('/');
|
||||
} catch (err) {
|
||||
console.error("Login error:", err);
|
||||
setError(
|
||||
err.message === "Invalid login credentials"
|
||||
? "Неверный email или пароль"
|
||||
: "Ошибка при входе. Попробуйте позже"
|
||||
);
|
||||
console.error('Login error:', err);
|
||||
setError(err.message || 'Ошибка входа');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@ -81,8 +56,8 @@ function LoginPage() {
|
||||
type="email"
|
||||
id="email"
|
||||
name="email"
|
||||
value={formData.email}
|
||||
onChange={handleChange}
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
className="input w-full"
|
||||
placeholder="your@email.com"
|
||||
required
|
||||
@ -109,8 +84,8 @@ function LoginPage() {
|
||||
type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
value={formData.password}
|
||||
onChange={handleChange}
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
className="input w-full"
|
||||
placeholder="••••••••"
|
||||
required
|
||||
@ -166,6 +141,6 @@ function LoginPage() {
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default LoginPage;
|
||||
|
Loading…
Reference in New Issue
Block a user