mirror of
https://github.com/yawaflua/SusMarket.git
synced 2025-12-10 04:29:32 +02:00
Authorization and registration coding
Coding of registration and authorization is finished. Also other fixes
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import React, { useState } from "react";
|
||||
import { motion } from "framer-motion";
|
||||
import axios from "axios";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
interface LoginMenuProps {
|
||||
toggleLoginMenu: () => void;
|
||||
@@ -7,15 +10,49 @@ interface LoginMenuProps {
|
||||
|
||||
export default function LoginMenu({ toggleLoginMenu }: LoginMenuProps): JSX.Element {
|
||||
const [isLoginMode, setIsLoginMode] = useState(true);
|
||||
const [login, setLogin] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const navigate = useNavigate();
|
||||
|
||||
const toggleMode = () => {
|
||||
setIsLoginMode(!isLoginMode);
|
||||
}
|
||||
|
||||
const handleAuth = async (isRegistering: boolean) => {
|
||||
try {
|
||||
let response;
|
||||
if (isRegistering) {
|
||||
// Регистрация пользователя
|
||||
const params = new URLSearchParams({
|
||||
login: login,
|
||||
password: password
|
||||
});
|
||||
response = await axios.get(`http://127.0.0.1:8000/api/post/user?${params.toString()}`);
|
||||
} else {
|
||||
// Вход в систему
|
||||
response = await axios.get(`http://127.0.0.1:8000/api/get/user?login=${encodeURIComponent(login)}&password=${encodeURIComponent(password)}`);
|
||||
}
|
||||
if (response.status === 200) {
|
||||
// Создание cookie файла
|
||||
Cookies.set('user', login, { expires: 1 }); // Cookie на 1 день
|
||||
// Перенаправление на страницу профиля
|
||||
navigate('/profile');
|
||||
toggleLoginMenu(); // Закрытие меню входа
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Ошибка при авторизации:', error);
|
||||
}
|
||||
}
|
||||
|
||||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
await handleAuth(!isLoginMode);
|
||||
}
|
||||
|
||||
return(
|
||||
<>
|
||||
<div className="background-blackout" onClick={toggleLoginMenu}></div>
|
||||
<form className="popup-login">
|
||||
<form className="popup-login" onSubmit={handleSubmit}>
|
||||
<div className="popup-login__top-container">
|
||||
<div className="top-container__headings-text">
|
||||
<h5 className="popup-menu__heading">
|
||||
@@ -27,8 +64,8 @@ export default function LoginMenu({ toggleLoginMenu }: LoginMenuProps): JSX.Elem
|
||||
</div>
|
||||
</div>
|
||||
<div className="popup-login__inputs-container">
|
||||
<input type="text" name="userName" id="userName" className="popup-login__name-input" placeholder="Логин"/>
|
||||
<input type="password" name="userPassword" id="userPassword" className="popup-login__password-input" placeholder="Пароль"/>
|
||||
<input type="text" name="userName" id="userName" className="popup-login__name-input" placeholder="Логин" value={login} onChange={(e) => setLogin(e.target.value)}/>
|
||||
<input type="password" name="userPassword" id="userPassword" className="popup-login__password-input" placeholder="Пароль" value={password} onChange={(e) => setPassword(e.target.value)}/>
|
||||
</div>
|
||||
<div className="popup-login__bottom-container">
|
||||
<p className="popup-login__prompt-url" onClick={toggleMode}>
|
||||
@@ -36,6 +73,7 @@ export default function LoginMenu({ toggleLoginMenu }: LoginMenuProps): JSX.Elem
|
||||
<u>{isLoginMode ? 'Зарегистрироваться' : 'Войти'}</u>
|
||||
</p>
|
||||
<motion.button
|
||||
type="submit"
|
||||
className="popup-login__login-button"
|
||||
whileTap={{scale: 0.98}}
|
||||
transition={{duration: 0.2, type: "spring"}}
|
||||
|
||||
Reference in New Issue
Block a user