## Features Added ### Admin Login Page (public/login.html) - Professional login UI with Bootstrap 5 - Email/password authentication - CSRF protection - Rate limiting protection - Session persistence (7 days) - Remember me functionality - Language: Spanish with translations ready ### Authentication Middleware (src/server/index.ts) - Session-based authentication using SQLite - bcrypt password hashing - CSRF token endpoint for form protection - Auth check on admin.html page load - Logout endpoint ### API Client Enhancements (public/js/api.js) - Added auth methods: login(), logout(), getMe(), getCsrfToken() - CRUD methods for all admin entities: - Properties: create, update, delete - Leads: get, update, delete - Testimonials: create, update, delete - FAQ: create, update, delete - Services: create, update, delete - Settings: get, update - Admin stats endpoint ### Comprehensive Seed Data (src/db/seed-comprehensive.ts) - 36 properties of all types: - 8 urban lands - 10 agricultural plots - 8 houses/villas - 10 apartments - Real Tenerife locations with coordinates - Spanish and Russian translations - 8 testimonials from international clients - 8 FAQ items (buying process, taxes, etc.) - 6 services offered - Admin user: admin@tenerifeprop.com / admin123 - Stock photos from Unsplash ### Tests (tests/auth.test.ts) - Authentication tests - Session management tests - Property CRUD tests - Input validation tests - XSS prevention tests - Email/phone validation tests ## Why These Changes 1. Security: Authentication protects admin routes from unauthorized access 2. Data: Seed data provides realistic content for testing and demo 3. UX: Professional login page improves user experience 4. Testing: Tests ensure reliability and catch regressions ## Breaking Changes None - all changes are additive ## Related Issues - Closes #28 (Admin Login Page) - Closes #29 (Seed Data Generation) - Closes #30 (Tests Implementation) ## Milestone Administrative Section Implementation (#51)
480 lines
16 KiB
HTML
480 lines
16 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="es">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="robots" content="noindex, nofollow">
|
|
|
|
<title>Iniciar Sesión | TenerifeProp Admin</title>
|
|
|
|
<!-- Bootstrap 5 CSS -->
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<!-- Bootstrap Icons -->
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css" rel="stylesheet">
|
|
<!-- Google Fonts -->
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
|
|
|
|
<style>
|
|
:root {
|
|
--primary: #1a5f4a;
|
|
--primary-light: #2d8f6f;
|
|
--primary-dark: #0d4535;
|
|
--secondary: #d4a853;
|
|
--accent: #e85d04;
|
|
}
|
|
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
|
background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
|
|
min-height: 100vh;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 20px;
|
|
}
|
|
|
|
.login-container {
|
|
width: 100%;
|
|
max-width: 420px;
|
|
}
|
|
|
|
.login-card {
|
|
background: #ffffff;
|
|
border-radius: 16px;
|
|
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.login-header {
|
|
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
|
|
padding: 40px 40px 30px;
|
|
text-align: center;
|
|
}
|
|
|
|
.login-logo {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 12px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.login-logo i {
|
|
font-size: 48px;
|
|
color: white;
|
|
}
|
|
|
|
.login-logo-text {
|
|
font-size: 28px;
|
|
font-weight: 800;
|
|
color: white;
|
|
letter-spacing: -0.5px;
|
|
}
|
|
|
|
.login-subtitle {
|
|
color: rgba(255, 255, 255, 0.9);
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.login-body {
|
|
padding: 40px;
|
|
}
|
|
|
|
.form-label {
|
|
font-weight: 600;
|
|
color: #1e293b;
|
|
margin-bottom: 8px;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.form-control {
|
|
border: 2px solid #e2e8f0;
|
|
border-radius: 10px;
|
|
padding: 12px 16px;
|
|
font-size: 15px;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.form-control:focus {
|
|
border-color: var(--primary);
|
|
box-shadow: 0 0 0 4px rgba(26, 95, 74, 0.1);
|
|
}
|
|
|
|
.input-group-text {
|
|
background: #f8fafc;
|
|
border: 2px solid #e2e8f0;
|
|
border-right: none;
|
|
border-radius: 10px 0 0 10px;
|
|
padding: 12px 16px;
|
|
}
|
|
|
|
.input-group .form-control {
|
|
border-left: none;
|
|
border-radius: 0 10px 10px 0;
|
|
}
|
|
|
|
.btn-login {
|
|
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
|
|
border: none;
|
|
border-radius: 10px;
|
|
padding: 14px 32px;
|
|
font-size: 16px;
|
|
font-weight: 600;
|
|
color: white;
|
|
width: 100%;
|
|
transition: all 0.3s;
|
|
margin-top: 10px;
|
|
}
|
|
|
|
.btn-login:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 10px 20px -10px rgba(26, 95, 74, 0.5);
|
|
}
|
|
|
|
.btn-login:disabled {
|
|
opacity: 0.7;
|
|
transform: none;
|
|
}
|
|
|
|
.alert {
|
|
border-radius: 10px;
|
|
border: none;
|
|
padding: 14px 18px;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.form-check-input:checked {
|
|
background-color: var(--primary);
|
|
border-color: var(--primary);
|
|
}
|
|
|
|
.divider {
|
|
display: flex;
|
|
align-items: center;
|
|
margin: 30px 0;
|
|
}
|
|
|
|
.divider::before,
|
|
.divider::after {
|
|
content: '';
|
|
flex: 1;
|
|
height: 1px;
|
|
background: #e2e8f0;
|
|
}
|
|
|
|
.divider span {
|
|
padding: 0 16px;
|
|
color: #94a3b8;
|
|
font-size: 12px;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.5px;
|
|
}
|
|
|
|
.back-link {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
color: #64748b;
|
|
text-decoration: none;
|
|
font-size: 14px;
|
|
margin-top: 20px;
|
|
transition: color 0.2s;
|
|
}
|
|
|
|
.back-link:hover {
|
|
color: var(--primary);
|
|
}
|
|
|
|
.password-toggle {
|
|
position: absolute;
|
|
right: 16px;
|
|
top: 50%;
|
|
transform: translateY(-50%);
|
|
background: none;
|
|
border: none;
|
|
color: #64748b;
|
|
cursor: pointer;
|
|
padding: 0;
|
|
}
|
|
|
|
.password-toggle:hover {
|
|
color: var(--primary);
|
|
}
|
|
|
|
.login-footer {
|
|
background: #f8fafc;
|
|
padding: 24px 40px;
|
|
border-top: 1px solid #e2e8f0;
|
|
text-align: center;
|
|
}
|
|
|
|
.login-footer a {
|
|
color: var(--primary);
|
|
text-decoration: none;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.login-footer a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
|
|
.spinner-border-sm {
|
|
width: 1rem;
|
|
height: 1rem;
|
|
}
|
|
|
|
@media (max-width: 480px) {
|
|
.login-header {
|
|
padding: 30px 20px 25px;
|
|
}
|
|
|
|
.login-body {
|
|
padding: 30px 20px;
|
|
}
|
|
|
|
.login-footer {
|
|
padding: 20px;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="login-container">
|
|
<div class="login-card">
|
|
<div class="login-header">
|
|
<div class="login-logo">
|
|
<i class="bi bi-house-door"></i>
|
|
<span class="login-logo-text">TenerifeProp</span>
|
|
</div>
|
|
<div class="login-subtitle">Panel de Administración</div>
|
|
</div>
|
|
|
|
<div class="login-body">
|
|
<div id="alertContainer"></div>
|
|
|
|
<form id="loginForm">
|
|
<input type="hidden" name="csrf_token" id="csrf_token">
|
|
|
|
<div class="mb-4">
|
|
<label for="email" class="form-label">
|
|
<i class="bi bi-envelope me-1"></i>
|
|
Correo electrónico
|
|
</label>
|
|
<div class="input-group">
|
|
<span class="input-group-text">
|
|
<i class="bi bi-envelope"></i>
|
|
</span>
|
|
<input
|
|
type="email"
|
|
class="form-control"
|
|
id="email"
|
|
name="email"
|
|
placeholder="admin@tenerifeprop.com"
|
|
required
|
|
autocomplete="email"
|
|
autofocus
|
|
>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label for="password" class="form-label">
|
|
<i class="bi bi-lock me-1"></i>
|
|
Contraseña
|
|
</label>
|
|
<div class="input-group">
|
|
<span class="input-group-text">
|
|
<i class="bi bi-lock"></i>
|
|
</span>
|
|
<input
|
|
type="password"
|
|
class="form-control"
|
|
id="password"
|
|
name="password"
|
|
placeholder="••••••••"
|
|
required
|
|
autocomplete="current-password"
|
|
>
|
|
<button type="button" class="password-toggle" onclick="togglePassword()">
|
|
<i class="bi bi-eye" id="toggleIcon"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-4 d-flex justify-content-between align-items-center">
|
|
<div class="form-check">
|
|
<input type="checkbox" class="form-check-input" id="remember" name="remember">
|
|
<label class="form-check-label" for="remember">
|
|
Recordarme
|
|
</label>
|
|
</div>
|
|
<a href="#" class="text-decoration-none" style="color: var(--primary); font-size: 14px;">
|
|
¿Olvidaste tu contraseña?
|
|
</a>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-login" id="loginBtn">
|
|
<span id="loginBtnText">Iniciar Sesión</span>
|
|
<span id="loginBtnSpinner" class="spinner-border spinner-border-sm d-none" role="status"></span>
|
|
</button>
|
|
</form>
|
|
|
|
<div class="back-link">
|
|
<i class="bi bi-arrow-left"></i>
|
|
<a href="/">Volver al sitio web</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="login-footer">
|
|
<small class="text-muted">
|
|
¿Necesitas ayuda? <a href="mailto:soporte@tenerifeprop.com">soporte@tenerifeprop.com</a>
|
|
</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Bootstrap JS -->
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
|
|
<script>
|
|
// Check if already logged in
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
try {
|
|
const res = await fetch('/api/auth/me')
|
|
if (res.ok) {
|
|
const data = await res.json()
|
|
if (data.success && data.data) {
|
|
window.location.href = '/admin.html'
|
|
return
|
|
}
|
|
}
|
|
} catch (e) {
|
|
// Not logged in, show login form
|
|
}
|
|
|
|
// Get CSRF token
|
|
try {
|
|
const csrfRes = await fetch('/api/csrf-token')
|
|
if (csrfRes.ok) {
|
|
const csrfData = await csrfRes.json()
|
|
document.getElementById('csrf_token').value = csrfData.token
|
|
}
|
|
} catch (e) {
|
|
console.error('Failed to get CSRF token')
|
|
}
|
|
|
|
// Focus email field
|
|
document.getElementById('email').focus()
|
|
})
|
|
|
|
// Toggle password visibility
|
|
function togglePassword() {
|
|
const password = document.getElementById('password')
|
|
const icon = document.getElementById('toggleIcon')
|
|
|
|
if (password.type === 'password') {
|
|
password.type = 'text'
|
|
icon.classList.remove('bi-eye')
|
|
icon.classList.add('bi-eye-slash')
|
|
} else {
|
|
password.type = 'password'
|
|
icon.classList.remove('bi-eye-slash')
|
|
icon.classList.add('bi-eye')
|
|
}
|
|
}
|
|
|
|
// Show alert
|
|
function showAlert(message, type = 'danger') {
|
|
const container = document.getElementById('alertContainer')
|
|
container.innerHTML = `
|
|
<div class="alert alert-${type} alert-dismissible fade show" role="alert">
|
|
<i class="bi bi-${type === 'danger' ? 'exclamation-triangle' : type === 'success' ? 'check-circle' : 'info-circle'} me-2"></i>
|
|
${message}
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
</div>
|
|
`
|
|
}
|
|
|
|
// Handle form submit
|
|
document.getElementById('loginForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault()
|
|
|
|
const email = document.getElementById('email').value.trim()
|
|
const password = document.getElementById('password').value
|
|
const remember = document.getElementById('remember').checked
|
|
|
|
// Validation
|
|
if (!email || !password) {
|
|
showAlert('Por favor complete todos los campos')
|
|
return
|
|
}
|
|
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
if (!emailRegex.test(email)) {
|
|
showAlert('Por favor ingrese un correo electrónico válido')
|
|
return
|
|
}
|
|
|
|
// Show loading state
|
|
const loginBtn = document.getElementById('loginBtn')
|
|
const loginBtnText = document.getElementById('loginBtnText')
|
|
const loginBtnSpinner = document.getElementById('loginBtnSpinner')
|
|
|
|
loginBtn.disabled = true
|
|
loginBtnText.textContent = 'Iniciando sesión...'
|
|
loginBtnSpinner.classList.remove('d-none')
|
|
|
|
try {
|
|
const res = await fetch('/api/auth/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ email, password })
|
|
})
|
|
|
|
const data = await res.json()
|
|
|
|
if (data.success) {
|
|
showAlert('¡Sesión iniciada correctamente! Redirigiendo...', 'success')
|
|
|
|
// Store user info in localStorage
|
|
localStorage.setItem('user', JSON.stringify(data.data))
|
|
|
|
// Redirect to admin
|
|
setTimeout(() => {
|
|
window.location.href = '/admin.html'
|
|
}, 500)
|
|
} else {
|
|
showAlert(data.error || 'Error al iniciar sesión. Verifique sus credenciales.')
|
|
loginBtn.disabled = false
|
|
loginBtnText.textContent = 'Iniciar Sesión'
|
|
loginBtnSpinner.classList.add('d-none')
|
|
}
|
|
} catch (error) {
|
|
showAlert('Error de conexión. Por favor intente más tarde.')
|
|
loginBtn.disabled = false
|
|
loginBtnText.textContent = 'Iniciar Sesión'
|
|
loginBtnSpinner.classList.add('d-none')
|
|
}
|
|
})
|
|
|
|
// Enter key submit
|
|
document.querySelectorAll('input').forEach(input => {
|
|
input.addEventListener('keypress', (e) => {
|
|
if (e.key === 'Enter') {
|
|
e.preventDefault()
|
|
document.getElementById('loginForm').dispatchEvent(new Event('submit'))
|
|
}
|
|
})
|
|
})
|
|
</script>
|
|
</body>
|
|
</html> |