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