feat: мульти-пользовательская авторизация через invite + email

- SQLite: таблицы users (email, password_hash, role, verified) + invites
- auth.py: register_user, verify_email, hash_password + legacy env user
- email_service.py: SMTP Timeweb (smtp.timeweb.ru:465) — код подтверждения
- API: POST /api/register, /api/verify-email, /api/admin/invite
- UI: /register?invite=TOKEN — 3-шаговая регистрация
- Admin: кнопка создать приглашение, список invites + users
- Обратная совместимость: env user (smeta) = admin

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-27 15:44:43 +10:00
co-authored by Claude Opus 4.6
parent 144a592e5e
commit 0469dd85e7
7 changed files with 523 additions and 19 deletions
+59 -2
View File
@@ -23,6 +23,21 @@ a:hover { text-decoration: underline; }
<h1>Dashboard</h1>
<div id="stats" style="display:flex;gap:1rem;margin-bottom:1.5rem;flex-wrap:wrap"></div>
<div id="daily" style="margin-bottom:1.5rem"></div>
<div style="display:flex;gap:.5rem;margin-bottom:1.5rem">
<button onclick="createInvite()" style="padding:.5rem 1rem;background:#c4463a;color:#fff;border:none;border-radius:6px;cursor:pointer;font-weight:600">Создать приглашение</button>
</div>
<div id="invite-result" style="margin-bottom:1rem;font-size:.85rem"></div>
<h2 style="font-size:1rem;margin-bottom:.5rem">Приглашения</h2>
<table id="invites" style="margin-bottom:1.5rem">
<tr><th>Ссылка</th><th>Создано</th><th>Использовано</th></tr>
</table>
<h2 style="font-size:1rem;margin-bottom:.5rem">Пользователи</h2>
<table id="users" style="margin-bottom:1.5rem">
<tr><th>Email</th><th>Роль</th><th>Подтверждён</th><th>Создан</th></tr>
</table>
<h2 style="font-size:1rem;margin-bottom:.5rem">Сессии</h2>
<table id="sessions">
<tr><th>Время</th><th>Пользователь</th><th>Сообщений</th><th>Последний вопрос</th><th></th></tr>
@@ -32,8 +47,50 @@ a:hover { text-decoration: underline; }
const token = localStorage.getItem('token');
if (!token) window.location.href = '/';
const H = {'Authorization': 'Bearer ' + token};
// Create invite
async function createInvite() {
const r = await fetch('/api/admin/invite', {method:'POST', headers: H});
const d = await r.json();
if (d.url) {
document.getElementById('invite-result').innerHTML = '<b>Ссылка:</b> <a href="' + d.url + '" target="_blank">' + d.url + '</a> <button onclick="navigator.clipboard.writeText(\'' + d.url + '\');this.textContent=\'Скопировано\'" style="padding:2px 8px;font-size:.75rem;cursor:pointer">Копировать</button>';
loadInvites();
} else {
document.getElementById('invite-result').textContent = d.detail || 'Ошибка';
}
}
// Load invites
function loadInvites() {
fetch('/api/admin/invites', {headers: H}).then(r=>r.json()).then(invites => {
const t = document.getElementById('invites');
t.innerHTML = '<tr><th>Ссылка</th><th>Создано</th><th>Использовано</th></tr>';
for (const i of invites) {
const d = new Date(i.created_at < 1e12 ? i.created_at*1000 : i.created_at);
const tr = document.createElement('tr');
const url = 'https://smeta.kazin.ru/register?invite=' + i.token;
const used = i.used_by || '—';
tr.innerHTML = '<td style="font-size:.75rem;word-break:break-all;max-width:300px"><a href="'+url+'" target="_blank">'+i.token.slice(0,12)+'...</a></td><td class="time">'+d.toLocaleString('ru-RU',{day:'2-digit',month:'2-digit',hour:'2-digit',minute:'2-digit'})+'</td><td style="font-size:.8rem">'+used+'</td>';
t.appendChild(tr);
}
});
}
loadInvites();
// Load users
fetch('/api/admin/users', {headers: H}).then(r=>r.json()).then(users => {
const t = document.getElementById('users');
for (const u of users) {
const d = new Date(u.created_at < 1e12 ? u.created_at*1000 : u.created_at);
const tr = document.createElement('tr');
tr.innerHTML = '<td>'+u.email+'</td><td>'+u.role+'</td><td>'+(u.verified?'Да':'Нет')+'</td><td class="time">'+d.toLocaleString('ru-RU',{day:'2-digit',month:'2-digit',hour:'2-digit',minute:'2-digit'})+'</td>';
t.appendChild(tr);
}
});
// Stats
fetch('/api/admin/stats', {headers: {'Authorization': 'Bearer ' + token}})
fetch('/api/admin/stats', {headers: H})
.then(r => r.json())
.then(data => {
const t = data.totals;
@@ -51,7 +108,7 @@ fetch('/api/admin/stats', {headers: {'Authorization': 'Bearer ' + token}})
});
// Sessions
fetch('/api/admin/sessions', {headers: {'Authorization': 'Bearer ' + token}})
fetch('/api/admin/sessions', {headers: H})
.then(r => r.json())
.then(sessions => {
const table = document.getElementById('sessions');