commit 12.01
This commit is contained in:
@@ -75,7 +75,6 @@
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<!-- Форма входа -->
|
||||
<div id="loginSection">
|
||||
<h2>Вход</h2>
|
||||
<div id="loginResult"></div>
|
||||
@@ -91,7 +90,6 @@
|
||||
<div class="toggle" onclick="showRegisterForm()">Нет аккаунта? Зарегистрироваться</div>
|
||||
</div>
|
||||
|
||||
<!-- Форма регистрации -->
|
||||
<div id="registerSection" style="display: none;">
|
||||
<h2>Регистрация</h2>
|
||||
<div id="registerResult"></div>
|
||||
@@ -117,106 +115,75 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Показать форму регистрации
|
||||
let token = localStorage.getItem('token');
|
||||
|
||||
// Проверка авторизации
|
||||
if (token) {
|
||||
window.location.href = 'index.html'; // ← ИСПРАВЛЕНО!
|
||||
}
|
||||
|
||||
// Регистрация
|
||||
async function registerUser() {
|
||||
const name = document.getElementById('nameReg').value;
|
||||
const email = document.getElementById('emailReg').value;
|
||||
const password = document.getElementById('passwordReg').value;
|
||||
const phone = document.getElementById('phoneReg').value || null;
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ name, email, password, phone })
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
if (res.ok) {
|
||||
alert('Регистрация успешна!');
|
||||
showLoginForm();
|
||||
} else {
|
||||
alert(data.message || 'Ошибка регистрации');
|
||||
}
|
||||
} catch (err) {
|
||||
alert('Ошибка сети');
|
||||
}
|
||||
}
|
||||
|
||||
// Вход
|
||||
async function loginUser() {
|
||||
const email = document.getElementById('emailLogin').value;
|
||||
const password = document.getElementById('passwordLogin').value;
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password })
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
if (res.ok) {
|
||||
localStorage.setItem('token', data.access_token);
|
||||
window.location.href = 'index.html'; // ← ИСПРАВЛЕНО!
|
||||
} else {
|
||||
alert(data.message || 'Неверный email или пароль');
|
||||
}
|
||||
} catch (err) {
|
||||
alert('Ошибка сети');
|
||||
}
|
||||
}
|
||||
|
||||
// Переключение форм
|
||||
function showRegisterForm() {
|
||||
document.getElementById('loginSection').style.display = 'none';
|
||||
document.getElementById('registerSection').style.display = 'block';
|
||||
}
|
||||
|
||||
// Показать форму входа
|
||||
function showLoginForm() {
|
||||
document.getElementById('registerSection').style.display = 'none';
|
||||
document.getElementById('loginSection').style.display = 'block';
|
||||
}
|
||||
|
||||
// Функция регистрации
|
||||
function registerUser() {
|
||||
// Собираем данные из формы
|
||||
var name = document.getElementById('nameReg').value;
|
||||
var email = document.getElementById('emailReg').value;
|
||||
var password = document.getElementById('passwordReg').value;
|
||||
var phone = document.getElementById('phoneReg').value || null;
|
||||
|
||||
// Подготавливаем данные для отправки
|
||||
var data = {
|
||||
name: name,
|
||||
email: email,
|
||||
password: password,
|
||||
phone: phone
|
||||
};
|
||||
|
||||
// Отправляем POST-запрос на сервер
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/api/register", true);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === 4) {
|
||||
var resultDiv = document.getElementById('registerResult');
|
||||
if (xhr.status === 200 || xhr.status === 201) {
|
||||
resultDiv.className = "message success";
|
||||
resultDiv.innerHTML = "Регистрация прошла успешно! Теперь войдите.";
|
||||
// Через 2 секунды переключим на форму входа
|
||||
setTimeout(function() {
|
||||
showLoginForm();
|
||||
resultDiv.innerHTML = "";
|
||||
}, 2000);
|
||||
} else {
|
||||
resultDiv.className = "message error";
|
||||
try {
|
||||
var response = JSON.parse(xhr.responseText);
|
||||
resultDiv.innerHTML = response.message || "Ошибка при регистрации";
|
||||
} catch (e) {
|
||||
resultDiv.innerHTML = "Неизвестная ошибка сервера";
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(data));
|
||||
}
|
||||
|
||||
// Функция входа
|
||||
function loginUser() {
|
||||
var email = document.getElementById('emailLogin').value;
|
||||
var password = document.getElementById('passwordLogin').value;
|
||||
|
||||
var data = {
|
||||
email: email,
|
||||
password: password
|
||||
};
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/api/login", true);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === 4) {
|
||||
var resultDiv = document.getElementById('loginResult');
|
||||
if (xhr.status === 200) {
|
||||
resultDiv.className = "message success";
|
||||
resultDiv.innerHTML = "Вход выполнен!";
|
||||
// Сохраняем токен в localStorage (чтобы потом использовать)
|
||||
var response = JSON.parse(xhr.responseText);
|
||||
localStorage.setItem('auth_token', response.token);
|
||||
// Перенаправляем на главную
|
||||
setTimeout(function() {
|
||||
window.location.href = "/";
|
||||
}, 1000);
|
||||
} else {
|
||||
resultDiv.className = "message error";
|
||||
try {
|
||||
var response = JSON.parse(xhr.responseText);
|
||||
resultDiv.innerHTML = response.message || "Неверный email или пароль";
|
||||
} catch (e) {
|
||||
resultDiv.innerHTML = "Ошибка при входе";
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(data));
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user