Виправлення помилок.

Додан новий метод авторізації в застосунок.
This commit is contained in:
2026-06-16 16:50:01 +03:00
parent 0b38f137bf
commit 9797ff6137
28 changed files with 2756 additions and 493 deletions
+563
View File
@@ -0,0 +1,563 @@
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Вхід - Sheep Service</title>
<link rel="icon" href="/img/favicon.ico">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
max-width: 500px;
width: 100%;
padding: 40px;
}
.logo {
text-align: center;
margin-bottom: 30px;
}
.logo img {
width: 80px;
height: 80px;
}
h1 {
text-align: center;
color: #333;
margin-bottom: 10px;
font-size: 28px;
}
.subtitle {
text-align: center;
color: #999;
margin-bottom: 30px;
font-size: 14px;
}
.tabs {
display: flex;
gap: 10px;
margin-bottom: 30px;
border-bottom: 2px solid #eee;
}
.tab-button {
flex: 1;
padding: 12px;
border: none;
background: none;
font-size: 16px;
cursor: pointer;
color: #999;
transition: all 0.3s;
border-bottom: 3px solid transparent;
margin-bottom: -2px;
font-weight: 500;
}
.tab-button.active {
color: #667eea;
border-bottom-color: #667eea;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #333;
font-weight: 500;
font-size: 14px;
}
input {
width: 100%;
padding: 12px;
border: 2px solid #eee;
border-radius: 6px;
font-size: 14px;
transition: border-color 0.3s;
}
input:focus {
outline: none;
border-color: #667eea;
}
button {
width: 100%;
padding: 12px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 6px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s;
margin-top: 10px;
}
button:hover:not(:disabled) {
transform: translateY(-2px);
}
button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.message {
padding: 12px;
border-radius: 6px;
margin-bottom: 20px;
font-size: 14px;
display: none;
}
.message.show {
display: block;
}
.message.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.message.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.loading {
text-align: center;
color: #999;
font-size: 14px;
margin: 20px 0;
}
.info {
background: #f0f4ff;
padding: 12px;
border-radius: 6px;
margin-bottom: 20px;
font-size: 13px;
color: #555;
line-height: 1.6;
}
.sessions-list {
margin-top: 20px;
}
.session-item {
background: #f8f9fa;
padding: 12px;
border-radius: 6px;
margin-bottom: 10px;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 13px;
}
.session-info {
flex: 1;
}
.session-device {
font-weight: 600;
color: #333;
}
.session-date {
font-size: 12px;
color: #999;
margin-top: 3px;
}
.session-status {
font-size: 11px;
padding: 3px 8px;
background: #d4edda;
color: #155724;
border-radius: 3px;
display: inline-block;
margin-top: 3px;
}
.session-action {
background: #f08080;
color: white;
padding: 6px 10px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
}
.session-action:hover {
background: #dc6b6b;
}
.logout-all {
background: #c82333;
width: 100%;
margin-top: 15px;
}
.logout-all:hover:not(:disabled) {
background: #a01a2a;
}
.logout-btn {
background: #6c757d;
margin-top: 15px;
}
.logout-btn:hover:not(:disabled) {
background: #5a6268;
}
.user-info {
background: #f0f4ff;
padding: 15px;
border-radius: 6px;
margin-bottom: 20px;
text-align: center;
}
.user-name {
font-weight: 600;
color: #333;
font-size: 16px;
margin-bottom: 5px;
}
.user-id {
font-size: 12px;
color: #999;
}
</style>
</head>
<body>
<div class="container">
<div class="logo">
<img src="/img/sheep-icon.png" alt="Sheep Service" onerror="this.style.display='none'">
</div>
<h1>🐑 Sheep Service</h1>
<p class="subtitle">Безпечна автентифікація через WebAuthn</p>
<div class="tabs">
<button class="tab-button active" data-tab="login">Вхід</button>
<button class="tab-button" data-tab="register">Реєстрація</button>
<button class="tab-button" data-tab="sessions">Сесії</button>
</div>
<!-- Login Tab -->
<div id="login" class="tab-content active">
<div id="loginMessage" class="message"></div>
<div class="info">
💡 Введіть UUID вашого облікового запису для входу.
</div>
<div class="form-group">
<label>UUID облікового запису</label>
<input type="text" id="loginUUID" placeholder="Введіть UUID" required>
</div>
<div class="form-group">
<label>Назва пристрою (опціонально)</label>
<input type="text" id="deviceName" placeholder="наприклад: iPhone 14 Pro">
</div>
<button id="loginBtn" onclick="handleLogin()">Увійти з WebAuthn</button>
</div>
<!-- Register Tab -->
<div id="register" class="tab-content">
<div id="registerMessage" class="message"></div>
<div class="info">
✅ Зареєструвати новий пристрій для входу через біометрію або PIN код.
</div>
<div class="form-group">
<label>UUID облікового запису</label>
<input type="text" id="registerUUID" placeholder="Введіть UUID" required>
</div>
<button id="registerBtn" onclick="handleRegister()">Зареєструвати WebAuthn</button>
</div>
<!-- Sessions Tab -->
<div id="sessions" class="tab-content">
<div id="sessionsMessage" class="message"></div>
<div id="userInfoContainer"></div>
<div id="sessionsList" class="sessions-list"></div>
<div id="sessionsActionContainer"></div>
</div>
</div>
<script src="/config.js"></script>
<script src="/js/auth-service.js"></script>
<script>
// Tab switching
document.querySelectorAll('.tab-button').forEach(btn => {
btn.addEventListener('click', () => {
const tabName = btn.dataset.tab;
document.querySelectorAll('.tab-content').forEach(tab => {
tab.classList.remove('active');
});
document.querySelectorAll('.tab-button').forEach(b => {
b.classList.remove('active');
});
document.getElementById(tabName).classList.add('active');
btn.classList.add('active');
if (tabName === 'sessions') {
loadSessions();
}
});
});
// Redirect if already authenticated
if (authService.isAuthenticated()) {
// Show sessions tab
document.querySelector('[data-tab="sessions"]').click();
}
function showMessage(elementId, message, type = 'success') {
const el = document.getElementById(elementId);
el.textContent = message;
el.className = `message ${type} show`;
setTimeout(() => {
el.classList.remove('show');
}, 5000);
}
async function handleRegister() {
const uuid = document.getElementById('registerUUID').value;
if (!uuid) {
showMessage('registerMessage', 'Будь ласка, введіть UUID', 'error');
return;
}
try {
document.getElementById('registerBtn').disabled = true;
// Get registration options
const { sheep_id, options } = await authService.startRegistration(uuid);
// Create credential
const credential = await navigator.credentials.create({
publicKey: {
...options,
challenge: new Uint8Array(atob(options.challenge).split('').map(c => c.charCodeAt(0))),
user: {
...options.user,
id: new Uint8Array(atob(options.user.id).split('').map(c => c.charCodeAt(0)))
}
}
});
if (!credential) {
throw new Error('Не вдалося створити облік');
}
// Verify registration
await authService.completeRegistration(sheep_id, credential, options.challenge);
showMessage('registerMessage', 'WebAuthn успішно зареєстрований! Тепер ви можете входити.', 'success');
document.getElementById('registerUUID').value = '';
setTimeout(() => {
document.querySelector('[data-tab="login"]').click();
}, 2000);
} catch (error) {
showMessage('registerMessage', `Помилка: ${error.message}`, 'error');
} finally {
document.getElementById('registerBtn').disabled = false;
}
}
async function handleLogin() {
const uuid = document.getElementById('loginUUID').value;
const deviceName = document.getElementById('deviceName').value;
if (!uuid) {
showMessage('loginMessage', 'Будь ласка, введіть UUID', 'error');
return;
}
try {
document.getElementById('loginBtn').disabled = true;
// Get authentication options
const { sheep_id, options } = await authService.startLogin(uuid);
// Get credential
const credential = await navigator.credentials.get({
publicKey: {
...options,
challenge: new Uint8Array(atob(options.challenge).split('').map(c => c.charCodeAt(0))),
allowCredentials: options.allowCredentials.map(cred => ({
...cred,
id: new Uint8Array(atob(cred.id).split('').map(c => c.charCodeAt(0)))
}))
}
});
if (!credential) {
throw new Error('Не вдалося отримати облік');
}
// Verify authentication
const result = await authService.completeLogin(sheep_id, credential, options.challenge, deviceName);
showMessage('loginMessage', `Ласкаво просимо, ${result.user.name}!`, 'success');
document.getElementById('loginUUID').value = '';
document.getElementById('deviceName').value = '';
// Redirect to main page
setTimeout(() => {
window.location.href = '/';
}, 2000);
} catch (error) {
showMessage('loginMessage', `Помилка: ${error.message}`, 'error');
} finally {
document.getElementById('loginBtn').disabled = false;
}
}
async function loadSessions() {
try {
if (!authService.isAuthenticated()) {
showMessage('sessionsMessage', 'Будь ласка, спочатку увійдіть', 'error');
document.getElementById('userInfoContainer').innerHTML = '';
document.getElementById('sessionsList').innerHTML = '';
document.getElementById('sessionsActionContainer').innerHTML = '';
return;
}
// Show user info
const user = authService.getCurrentUser();
if (user) {
document.getElementById('userInfoContainer').innerHTML = `
<div class="user-info">
<div class="user-name">${user.name}</div>
<div class="user-id">ID: ${user.id}</div>
</div>
`;
}
// Load sessions
const sessions = await authService.getSessions();
if (sessions.length === 0) {
document.getElementById('sessionsList').innerHTML = '<p style="text-align: center; color: #999;">Немає активних сесій</p>';
} else {
const listHtml = sessions.map(session => `
<div class="session-item">
<div class="session-info">
<div class="session-device">📱 ${session.device_name}</div>
<div class="session-date">Створена: ${new Date(session.created_at).toLocaleString('uk-UA')}</div>
<div class="session-date">IP: ${session.ip_address}</div>
<span class="session-status">${session.status.toUpperCase()}</span>
</div>
<button class="session-action" onclick="deleteSession(${session.id})">Видалити</button>
</div>
`).join('');
document.getElementById('sessionsList').innerHTML = listHtml;
}
// Show action buttons
document.getElementById('sessionsActionContainer').innerHTML = `
<button class="logout-all" onclick="logoutAll()">Вихід зі всіх пристроїв</button>
<button class="logout-btn" onclick="logout()">Вийти з цього пристрою</button>
`;
} catch (error) {
showMessage('sessionsMessage', `Помилка: ${error.message}`, 'error');
}
}
async function deleteSession(sessionId) {
try {
await authService.deleteSession(sessionId);
loadSessions();
} catch (error) {
showMessage('sessionsMessage', `Помилка: ${error.message}`, 'error');
}
}
async function logoutAll() {
if (!confirm('Ви впевнені? Це видалить вас зі всіх пристроїв.')) {
return;
}
try {
await authService.logoutAll();
showMessage('sessionsMessage', 'Ви вийшли зі всіх пристроїв', 'success');
setTimeout(() => {
window.location.href = '/login.html';
}, 2000);
} catch (error) {
showMessage('sessionsMessage', `Помилка: ${error.message}`, 'error');
}
}
async function logout() {
try {
await authService.logout();
showMessage('sessionsMessage', 'Ви вийшли', 'success');
setTimeout(() => {
window.location.href = '/login.html';
}, 1000);
} catch (error) {
showMessage('sessionsMessage', `Помилка: ${error.message}`, 'error');
}
}
// Auto load sessions on init
if (authService.isAuthenticated()) {
loadSessions();
}
</script>
</body>
</html>