Files
orion/static/js/admin/login.js

87 lines
2.5 KiB
JavaScript

// Admin Login Component
function adminLogin() {
return {
credentials: {
username: '',
password: ''
},
loading: false,
error: null,
success: null,
errors: {},
init() {
// Check if already logged in
this.checkExistingAuth();
},
checkExistingAuth() {
if (Auth.isAuthenticated() && Auth.isAdmin()) {
window.location.href = '/static/admin/dashboard.html';
}
},
clearErrors() {
this.error = null;
this.errors = {};
},
validateForm() {
this.clearErrors();
let isValid = true;
if (!this.credentials.username.trim()) {
this.errors.username = 'Username is required';
isValid = false;
}
if (!this.credentials.password) {
this.errors.password = 'Password is required';
isValid = false;
}
return isValid;
},
async handleLogin() {
if (!this.validateForm()) {
return;
}
this.loading = true;
this.clearErrors();
try {
const response = await apiClient.post('/admin/auth/login', {
username: this.credentials.username.trim(),
password: this.credentials.password
});
// Check if user is admin
if (response.user.role !== 'admin') {
throw new Error('Access denied. Admin privileges required.');
}
// Store authentication data
localStorage.setItem('admin_token', response.access_token);
localStorage.setItem('admin_user', JSON.stringify(response.user));
// Show success message
this.success = 'Login successful! Redirecting...';
Utils.showToast('Login successful!', 'success', 2000);
// Redirect after short delay
setTimeout(() => {
window.location.href = '/static/admin/dashboard.html';
}, 1000);
} catch (error) {
console.error('Login error:', error);
this.error = error.message || 'Login failed. Please check your credentials.';
Utils.showToast(this.error, 'error');
} finally {
this.loading = false;
}
}
}
}