396 lines
11 KiB
HTML
396 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="id">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Login - Lost & Found</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
:root {
|
|
--primary: #2563eb;
|
|
--primary-dark: #1e40af;
|
|
--danger: #ef4444;
|
|
--success: #10b981;
|
|
--light: #f8fafc;
|
|
--dark: #1e293b;
|
|
}
|
|
|
|
body {
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 20px;
|
|
}
|
|
|
|
.login-container {
|
|
background: white;
|
|
border-radius: 20px;
|
|
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
|
overflow: hidden;
|
|
max-width: 450px;
|
|
width: 100%;
|
|
}
|
|
|
|
.login-header {
|
|
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
|
|
color: white;
|
|
padding: 40px 30px;
|
|
text-align: center;
|
|
}
|
|
|
|
.login-header h1 {
|
|
font-size: 2rem;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.login-header p {
|
|
opacity: 0.9;
|
|
}
|
|
|
|
.login-body {
|
|
padding: 40px 30px;
|
|
}
|
|
|
|
.form-group {
|
|
margin-bottom: 25px;
|
|
}
|
|
|
|
.form-group label {
|
|
display: block;
|
|
margin-bottom: 8px;
|
|
color: var(--dark);
|
|
font-weight: 600;
|
|
}
|
|
|
|
.form-group input {
|
|
width: 100%;
|
|
padding: 12px 15px;
|
|
border: 2px solid #e2e8f0;
|
|
border-radius: 10px;
|
|
font-size: 1rem;
|
|
transition: border-color 0.3s;
|
|
}
|
|
|
|
.form-group input:focus {
|
|
outline: none;
|
|
border-color: var(--primary);
|
|
}
|
|
|
|
.form-group input.error {
|
|
border-color: var(--danger);
|
|
}
|
|
|
|
.error-message {
|
|
color: var(--danger);
|
|
font-size: 0.85rem;
|
|
margin-top: 5px;
|
|
display: none;
|
|
}
|
|
|
|
.error-message.show {
|
|
display: block;
|
|
}
|
|
|
|
.alert {
|
|
padding: 12px 15px;
|
|
border-radius: 10px;
|
|
margin-bottom: 20px;
|
|
display: none;
|
|
}
|
|
|
|
.alert.show {
|
|
display: block;
|
|
}
|
|
|
|
.alert-error {
|
|
background: #fee;
|
|
color: var(--danger);
|
|
border: 1px solid var(--danger);
|
|
}
|
|
|
|
.alert-success {
|
|
background: #efe;
|
|
color: var(--success);
|
|
border: 1px solid var(--success);
|
|
}
|
|
|
|
.btn-login {
|
|
width: 100%;
|
|
padding: 15px;
|
|
background: var(--primary);
|
|
color: white;
|
|
border: none;
|
|
border-radius: 10px;
|
|
font-size: 1.1rem;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
}
|
|
|
|
.btn-login:hover {
|
|
background: var(--primary-dark);
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 10px 20px rgba(37, 99, 235, 0.3);
|
|
}
|
|
|
|
.btn-login:disabled {
|
|
background: #94a3b8;
|
|
cursor: not-allowed;
|
|
transform: none;
|
|
}
|
|
|
|
.divider {
|
|
text-align: center;
|
|
margin: 25px 0;
|
|
color: #64748b;
|
|
position: relative;
|
|
}
|
|
|
|
.divider::before,
|
|
.divider::after {
|
|
content: '';
|
|
position: absolute;
|
|
top: 50%;
|
|
width: 45%;
|
|
height: 1px;
|
|
background: #e2e8f0;
|
|
}
|
|
|
|
.divider::before {
|
|
left: 0;
|
|
}
|
|
|
|
.divider::after {
|
|
right: 0;
|
|
}
|
|
|
|
.register-link {
|
|
text-align: center;
|
|
margin-top: 20px;
|
|
}
|
|
|
|
.register-link a {
|
|
color: var(--primary);
|
|
text-decoration: none;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.register-link a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
|
|
.back-home {
|
|
text-align: center;
|
|
margin-top: 15px;
|
|
}
|
|
|
|
.back-home a {
|
|
color: #64748b;
|
|
text-decoration: none;
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.back-home a:hover {
|
|
color: var(--primary);
|
|
}
|
|
|
|
.loading {
|
|
display: inline-block;
|
|
width: 20px;
|
|
height: 20px;
|
|
border: 3px solid rgba(255,255,255,.3);
|
|
border-radius: 50%;
|
|
border-top-color: white;
|
|
animation: spin 1s ease-in-out infinite;
|
|
}
|
|
|
|
@keyframes spin {
|
|
to { transform: rotate(360deg); }
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.login-container {
|
|
margin: 10px;
|
|
}
|
|
|
|
.login-header {
|
|
padding: 30px 20px;
|
|
}
|
|
|
|
.login-body {
|
|
padding: 30px 20px;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="login-container">
|
|
<div class="login-header">
|
|
<h1>🔐 Login</h1>
|
|
<p>Masuk ke Lost & Found System</p>
|
|
</div>
|
|
|
|
<div class="login-body">
|
|
<div id="alertBox" class="alert"></div>
|
|
|
|
<form id="loginForm">
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" id="email" name="email" placeholder="mahasiswa@example.com" required>
|
|
<div class="error-message" id="emailError">Email tidak valid</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" id="password" name="password" placeholder="********" required>
|
|
<div class="error-message" id="passwordError">Password minimal 6 karakter</div>
|
|
</div>
|
|
|
|
<button type="submit" class="btn-login" id="loginBtn">
|
|
Login
|
|
</button>
|
|
</form>
|
|
|
|
<div class="divider">atau</div>
|
|
|
|
<div class="register-link">
|
|
<p>Belum punya akun? <a href="/register">Register disini</a></p>
|
|
</div>
|
|
|
|
<div class="back-home">
|
|
<a href="/">← Kembali ke Beranda</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const API_URL = 'http://localhost:8080/api';
|
|
|
|
const loginForm = document.getElementById('loginForm');
|
|
const emailInput = document.getElementById('email');
|
|
const passwordInput = document.getElementById('password');
|
|
const loginBtn = document.getElementById('loginBtn');
|
|
const alertBox = document.getElementById('alertBox');
|
|
|
|
// Validasi Email
|
|
function validateEmail(email) {
|
|
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
return re.test(email);
|
|
}
|
|
|
|
// Show Alert
|
|
function showAlert(message, type = 'error') {
|
|
alertBox.textContent = message;
|
|
alertBox.className = `alert alert-${type} show`;
|
|
setTimeout(() => {
|
|
alertBox.classList.remove('show');
|
|
}, 5000);
|
|
}
|
|
|
|
// Clear errors
|
|
function clearErrors() {
|
|
document.querySelectorAll('.error-message').forEach(el => {
|
|
el.classList.remove('show');
|
|
});
|
|
document.querySelectorAll('input').forEach(el => {
|
|
el.classList.remove('error');
|
|
});
|
|
}
|
|
|
|
// Form submit
|
|
loginForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
clearErrors();
|
|
|
|
const email = emailInput.value.trim();
|
|
const password = passwordInput.value;
|
|
|
|
// Validasi frontend
|
|
let hasError = false;
|
|
|
|
if (!validateEmail(email)) {
|
|
emailInput.classList.add('error');
|
|
document.getElementById('emailError').classList.add('show');
|
|
hasError = true;
|
|
}
|
|
|
|
if (password.length < 6) {
|
|
passwordInput.classList.add('error');
|
|
document.getElementById('passwordError').classList.add('show');
|
|
hasError = true;
|
|
}
|
|
|
|
if (hasError) return;
|
|
|
|
// Disable button dan tampilkan loading
|
|
loginBtn.disabled = true;
|
|
loginBtn.innerHTML = '<span class="loading"></span> Logging in...';
|
|
|
|
try {
|
|
const response = await fetch(`${API_URL}/login`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (response.ok) {
|
|
// Simpan token dan user info
|
|
localStorage.setItem('token', data.token);
|
|
localStorage.setItem('user', JSON.stringify(data.user));
|
|
|
|
showAlert('Login berhasil! Mengalihkan...', 'success');
|
|
|
|
// Redirect berdasarkan role - REFACTORED: URL lebih simple
|
|
setTimeout(() => {
|
|
const role = data.user.role;
|
|
if (role === 'admin') {
|
|
window.location.href = '/admin'; // ✅ REFACTORED
|
|
} else if (role === 'manager') {
|
|
window.location.href = '/manager'; // ✅ REFACTORED
|
|
} else {
|
|
window.location.href = '/user'; // ✅ REFACTORED
|
|
}
|
|
}, 1000);
|
|
} else {
|
|
showAlert(data.error || 'Email atau password salah!');
|
|
}
|
|
} catch (error) {
|
|
console.error('Login error:', error);
|
|
showAlert('Terjadi kesalahan. Pastikan server berjalan di port 8080');
|
|
} finally {
|
|
loginBtn.disabled = false;
|
|
loginBtn.textContent = 'Login';
|
|
}
|
|
});
|
|
|
|
// Check jika sudah login - FIXED: gunakan underscore
|
|
window.addEventListener('load', () => {
|
|
const token = localStorage.getItem('token');
|
|
const user = JSON.parse(localStorage.getItem('user') || '{}');
|
|
|
|
if (token && user.role) {
|
|
// Redirect ke dashboard sesuai role
|
|
if (user.role === 'admin') {
|
|
window.location.href = '/admin'; // ✅ FIXED
|
|
} else if (user.role === 'manager') {
|
|
window.location.href = '/manager'; // ✅ FIXED
|
|
} else {
|
|
window.location.href = '/user'; // ✅ FIXED
|
|
}
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html> |