-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-cloud.js
More file actions
96 lines (89 loc) · 5.26 KB
/
Copy pathauth-cloud.js
File metadata and controls
96 lines (89 loc) · 5.26 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
// auth-cloud.js
// Подменяет логин и регистрацию на облачные, не трогая index.html
(function() {
// 🔑 ВСТАВЬ СЮДА URL СВОЕГО WORKER
const WORKER_URL = 'https://course-auth.chatbotsmarketolog.workers.dev';
const ADMIN_SECRET = 'super-secret-kristina'; // Тот же, что в воркере
// Ждём полной загрузки страницы
window.addEventListener('load', () => {
// Если пользователь уже вошёл ранее (есть сессия)
const sessionUser = localStorage.getItem('courseCloudUser');
if (sessionUser && document.getElementById('loginError')) {
// Автоматически скрываем форму входа и показываем курс
const loginPage = document.querySelector('.login-container') || document.getElementById('page-login');
if (loginPage) loginPage.style.display = 'none';
const dashboard = document.getElementById('page-dashboard');
if (dashboard) {
dashboard.style.display = 'block';
document.getElementById('dashboardUserName').textContent = sessionUser;
}
}
// Перехватываем функцию логина из index.html
if (typeof handleLogin === 'function') {
window._originalHandleLogin = handleLogin;
window.handleLogin = async function() {
const u = document.getElementById('loginUsername').value.trim();
const p = document.getElementById('loginPassword').value.trim();
const errEl = document.getElementById('loginError');
if (!u || !p) {
errEl.textContent = 'Заполните все поля';
errEl.style.display = 'block';
return;
}
errEl.textContent = 'Проверяем доступ в облаке...';
errEl.style.display = 'block';
try {
const res = await fetch(WORKER_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'login', username: u, password: p })
});
const data = await res.json();
if (data.success) {
localStorage.setItem('courseCloudUser', data.username);
errEl.style.display = 'none';
// Запускаем стандартный переход в курс из твоего index.html
if (typeof showPage === 'function') showPage('page-dashboard');
if (typeof showToast === 'function') showToast(`Добро пожаловать, ${data.username}! 🍒`);
} else {
errEl.textContent = data.error || 'Неверный логин или пароль';
errEl.style.display = 'block';
}
} catch (e) {
errEl.textContent = 'Нет связи с сервером. Проверьте интернет.';
errEl.style.display = 'block';
}
};
}
// Перехватываем функцию регистрации из index.html (если она там есть)
if (typeof registerStudent === 'function') {
window._originalRegisterStudent = registerStudent;
window.registerStudent = async function() {
const name = document.getElementById('newStudentName')?.value.trim();
const pass = document.getElementById('newStudentPass')?.value.trim();
if (!name || !pass) {
if (typeof showToast === 'function') showToast('Заполни имя и пароль');
return;
}
if (typeof showToast === 'function') showToast('Создаём аккаунт в облаке...');
try {
const res = await fetch(WORKER_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'register', username: name, password: pass, adminSecret: ADMIN_SECRET })
});
const data = await res.json();
if (data.success) {
if (typeof showToast === 'function') showToast(`Ученик "${name}" создан! ✅`);
if (document.getElementById('newStudentName')) document.getElementById('newStudentName').value = '';
if (document.getElementById('newStudentPass')) document.getElementById('newStudentPass').value = '';
} else {
if (typeof showToast === 'function') showToast('Ошибка: ' + (data.error || 'Не удалось создать'));
}
} catch (e) {
if (typeof showToast === 'function') showToast('Ошибка соединения');
}
};
}
});
})();