-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
122 lines (110 loc) · 5.44 KB
/
index.html
File metadata and controls
122 lines (110 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Авторизация</title>
<style>
/* Стили остаются прежними */
</style>
<!-- Подключаем Firebase -->
<script src="https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.0/firebase-database.js"></script>
</head>
<body>
<div class="container">
<h1>Авторизация</h1>
<!-- Форма регистрации -->
<form id="register-form" onsubmit="register(event)">
<label for="register-username">Имя пользователя</label>
<input type="text" id="register-username" required>
<label for="register-password">Пароль</label>
<input type="password" id="register-password" required>
<button type="submit">Зарегистрироваться</button>
</form>
<div class="switch-form">
<span>Уже есть аккаунт? <a href="#" onclick="toggleForms()">Войти</a></span>
</div>
<!-- Форма входа -->
<form id="login-form" style="display: none;" onsubmit="login(event)">
<label for="login-username">Имя пользователя</label>
<input type="text" id="login-username" required>
<label for="login-password">Пароль</label>
<input type="password" id="login-password" required>
<button type="submit">Войти</button>
</form>
<div class="switch-form" style="display: none;">
<span>Нет аккаунта? <a href="#" onclick="toggleForms()">Зарегистрироваться</a></span>
</div>
</div>
<script>
const firebaseConfig = {
apiKey: "AIzaSyDDlKFWzd9phmxxd1X4ILVRA_81fWfGzL8",
authDomain: "nt-clicker.firebaseapp.com",
databaseURL: "https://nt-clicker-default-rtdb.firebaseio.com",
projectId: "nt-clicker",
storageBucket: "nt-clicker.appspot.com",
messagingSenderId: "453868400021",
appId: "1:453868400021:web:40330159023b35735e5a9c",
measurementId: "G-F64CW9CQPT"
};
// Инициализация Firebase
const app = firebase.initializeApp(firebaseConfig);
const database = firebase.database();
function toggleForms() {
const registerForm = document.getElementById('register-form');
const loginForm = document.getElementById('login-form');
const switchToLogin = document.querySelectorAll('.switch-form')[0];
const switchToRegister = document.querySelectorAll('.switch-form')[1];
if (registerForm.style.display === 'none') {
registerForm.style.display = 'block';
loginForm.style.display = 'none';
switchToLogin.style.display = 'block';
switchToRegister.style.display = 'none';
} else {
registerForm.style.display = 'none';
loginForm.style.display = 'block';
switchToLogin.style.display = 'none';
switchToRegister.style.display = 'block';
}
}
function register(event) {
event.preventDefault();
const username = document.getElementById('register-username').value;
const password = document.getElementById('register-password').value;
// Сохраняем данные в Firebase
database.ref('users/' + username).set({
password: password,
balance: 0 // Начальный баланс
}).then(() => {
alert('Регистрация прошла успешно!');
window.location.href = 'clicker.html'; // Перенаправление на clicker.html
}).catch(error => {
alert('Ошибка регистрации: ' + error.message);
});
}
function login(event) {
event.preventDefault();
const username = document.getElementById('login-username').value;
const password = document.getElementById('login-password').value;
// Проверка существования пользователя
database.ref('users/' + username).once('value').then(snapshot => {
if (snapshot.exists()) {
const userData = snapshot.val();
if (userData.password === password) {
alert('Вход успешен!');
localStorage.setItem('username', username); // Сохраняем имя пользователя в localStorage
window.location.href = 'clicker.html'; // Перенаправление на clicker.html
} else {
alert('Неверный пароль');
}
} else {
alert('Пользователь не найден');
}
}).catch(error => {
alert('Ошибка входа: ' + error.message);
});
}
</script>
</body>
</html>