2025-11-11 01:05:13 +01:00
|
|
|
import app from '../app.js';
|
|
|
|
|
|
|
|
|
|
const api = app.getAPI();
|
|
|
|
|
const logger = app.getLogger();
|
2025-11-09 23:29:07 +01:00
|
|
|
|
|
|
|
|
export class LoginView extends HTMLElement {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.render();
|
|
|
|
|
this.attachListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
this.innerHTML = `
|
|
|
|
|
<div class="auth-container">
|
|
|
|
|
<div class="auth-box">
|
|
|
|
|
<h1>RBox</h1>
|
|
|
|
|
<p class="auth-subtitle">Self-hosted cloud storage</p>
|
|
|
|
|
|
|
|
|
|
<div class="auth-tabs">
|
|
|
|
|
<button class="auth-tab active" data-tab="login">Login</button>
|
2025-11-10 15:46:40 +01:00
|
|
|
<button class="auth-tab" data-tab="register">Sign Up</button>
|
2025-11-09 23:29:07 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<form id="login-form" class="auth-form">
|
|
|
|
|
<input type="text" name="username" placeholder="Username" required class="input-field">
|
|
|
|
|
<input type="password" name="password" placeholder="Password" required class="input-field">
|
|
|
|
|
<button type="submit" class="button button-primary">Login</button>
|
|
|
|
|
<div class="error-message" id="login-error"></div>
|
|
|
|
|
</form>
|
|
|
|
|
|
|
|
|
|
<form id="register-form" class="auth-form" style="display: none;">
|
|
|
|
|
<input type="text" name="username" placeholder="Username" required class="input-field">
|
|
|
|
|
<input type="email" name="email" placeholder="Email" required class="input-field">
|
|
|
|
|
<input type="password" name="password" placeholder="Password" required class="input-field">
|
|
|
|
|
<button type="submit" class="button button-primary">Register</button>
|
|
|
|
|
<div class="error-message" id="register-error"></div>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attachListeners() {
|
|
|
|
|
const tabs = this.querySelectorAll('.auth-tab');
|
|
|
|
|
tabs.forEach(tab => {
|
|
|
|
|
tab.addEventListener('click', () => this.switchTab(tab.dataset.tab));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.querySelector('#login-form').addEventListener('submit', (e) => this.handleLogin(e));
|
|
|
|
|
this.querySelector('#register-form').addEventListener('submit', (e) => this.handleRegister(e));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switchTab(tab) {
|
|
|
|
|
const tabs = this.querySelectorAll('.auth-tab');
|
|
|
|
|
tabs.forEach(t => t.classList.remove('active'));
|
|
|
|
|
this.querySelector(`[data-tab="${tab}"]`).classList.add('active');
|
|
|
|
|
|
|
|
|
|
const loginForm = this.querySelector('#login-form');
|
|
|
|
|
const registerForm = this.querySelector('#register-form');
|
|
|
|
|
|
|
|
|
|
if (tab === 'login') {
|
|
|
|
|
loginForm.style.display = 'block';
|
|
|
|
|
registerForm.style.display = 'none';
|
|
|
|
|
} else {
|
|
|
|
|
loginForm.style.display = 'none';
|
|
|
|
|
registerForm.style.display = 'block';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handleLogin(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const form = e.target;
|
|
|
|
|
const formData = new FormData(form);
|
|
|
|
|
const username = formData.get('username');
|
|
|
|
|
const password = formData.get('password');
|
|
|
|
|
const errorDiv = this.querySelector('#login-error');
|
|
|
|
|
|
|
|
|
|
try {
|
2025-11-11 01:05:13 +01:00
|
|
|
logger.info('Login attempt started', { username });
|
2025-11-09 23:29:07 +01:00
|
|
|
await api.login(username, password);
|
2025-11-11 01:05:13 +01:00
|
|
|
logger.info('Login successful, dispatching auth-success event');
|
2025-11-09 23:29:07 +01:00
|
|
|
this.dispatchEvent(new CustomEvent('auth-success'));
|
|
|
|
|
} catch (error) {
|
2025-11-11 01:05:13 +01:00
|
|
|
logger.error('Login failed', { username, error: error.message });
|
2025-11-09 23:29:07 +01:00
|
|
|
errorDiv.textContent = error.message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handleRegister(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const form = e.target;
|
|
|
|
|
const formData = new FormData(form);
|
|
|
|
|
const username = formData.get('username');
|
|
|
|
|
const email = formData.get('email');
|
|
|
|
|
const password = formData.get('password');
|
|
|
|
|
const errorDiv = this.querySelector('#register-error');
|
|
|
|
|
|
|
|
|
|
try {
|
2025-11-11 01:05:13 +01:00
|
|
|
logger.info('Registration attempt started', { username, email });
|
2025-11-09 23:29:07 +01:00
|
|
|
await api.register(username, email, password);
|
2025-11-11 01:05:13 +01:00
|
|
|
logger.info('Registration successful, dispatching auth-success event');
|
2025-11-09 23:29:07 +01:00
|
|
|
this.dispatchEvent(new CustomEvent('auth-success'));
|
|
|
|
|
} catch (error) {
|
2025-11-11 01:05:13 +01:00
|
|
|
logger.error('Registration failed', { username, email, error: error.message });
|
2025-11-09 23:29:07 +01:00
|
|
|
errorDiv.textContent = error.message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
customElements.define('login-view', LoginView);
|