This commit is contained in:
Ken
2026-01-19 09:33:35 +07:00
parent 374dc12b2d
commit 70838a4bc1
103 changed files with 16929 additions and 2 deletions

252
public/js/login.js Normal file
View File

@@ -0,0 +1,252 @@
// API Base URL - thay đổi theo cấu hình của bạn
const API_BASE_URL = '/api/';
const loginForm = document.getElementById('loginForm');
const loginBtn = document.getElementById('loginBtn');
const registerBtn = document.getElementById('registerBtn');
const verifyTokenBtn = document.getElementById('verifyTokenBtn');
const logoutBtn = document.getElementById('logoutBtn');
const getAllUsersBtn = document.getElementById('getAllUsersBtn');
const testHealthBtn = document.getElementById('testHealthBtn');
const responseContainer = document.getElementById('responseContainer');
// Load token from localStorage
let authToken = localStorage.getItem('token');
// Helper function to display response
function displayResponse(title, data, status) {
const statusClass = status >= 200 && status < 300 ? 'success' : 'error';
const statusText = status >= 200 && status < 300 ? 'Success' : 'Error';
responseContainer.innerHTML = `
<div class="response-content">
<div class="response-title">${title}</div>
<span class="status ${statusClass}">Status: ${status} ${statusText}</span>
<pre>${JSON.stringify(data, null, 2)}</pre>
</div>
`;
}
// Helper function to show loading
function showLoading(btn) {
const originalText = btn.innerHTML;
btn.disabled = true;
btn.innerHTML = originalText + '<span class="loading"></span>';
return originalText;
}
function hideLoading(btn, originalText) {
btn.disabled = false;
btn.innerHTML = originalText;
}
// Login form submission
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const originalText = showLoading(loginBtn);
try {
// Call login endpoint
const response = await fetch(`${API_BASE_URL}auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: username,
password: password,
}),
});
const data = await response.json();
displayResponse('Login Response', data, response.status);
if (response.ok && data.data && data.data.token) {
// Store token
authToken = data.data.token;
localStorage.setItem('token', authToken);
alert('✅ Đăng nhập thành công!');
}
} catch (error) {
displayResponse('Login Error', {
error: error.message,
note: 'Không thể kết nối đến server. Hãy đảm bảo server đang chạy.'
}, 500);
console.error('Error:', error);
} finally {
hideLoading(loginBtn, originalText);
}
});
// Register button
registerBtn.addEventListener('click', async () => {
const username = prompt('Nhập username:');
if (!username) return;
const email = prompt('Nhập email:');
if (!email) return;
const password = prompt('Nhập password:');
if (!password) return;
const full_name = prompt('Nhập họ tên (optional):');
const originalText = showLoading(registerBtn);
try {
const response = await fetch(`${API_BASE_URL}auth/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username,
email,
password,
full_name,
}),
});
const data = await response.json();
displayResponse('Register Response', data, response.status);
if (response.ok) {
alert('✅ Đăng ký thành công! Bạn có thể đăng nhập ngay.');
}
} catch (error) {
displayResponse('Register Error', {
error: error.message
}, 500);
console.error('Error:', error);
} finally {
hideLoading(registerBtn, originalText);
}
});
// Verify token button
verifyTokenBtn.addEventListener('click', async () => {
const originalText = showLoading(verifyTokenBtn);
try {
if (!authToken) {
displayResponse('Verify Token Error', {
error: 'Chưa có token. Vui lòng đăng nhập trước.'
}, 401);
return;
}
const response = await fetch(`${API_BASE_URL}auth/verify-token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`,
},
});
const data = await response.json();
displayResponse('Verify Token Response', data, response.status);
} catch (error) {
displayResponse('Verify Token Error', {
error: error.message
}, 500);
console.error('Error:', error);
} finally {
hideLoading(verifyTokenBtn, originalText);
}
});
// Logout button
logoutBtn.addEventListener('click', async () => {
const originalText = showLoading(logoutBtn);
try {
if (!authToken) {
displayResponse('Logout Error', {
error: 'Chưa có token. Vui lòng đăng nhập trước.'
}, 401);
return;
}
const response = await fetch(`${API_BASE_URL}auth/logout`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`,
},
});
const data = await response.json();
displayResponse('Logout Response', data, response.status);
if (response.ok) {
authToken = null;
localStorage.removeItem('token');
alert('✅ Đăng xuất thành công!');
}
} catch (error) {
displayResponse('Logout Error', {
error: error.message
}, 500);
console.error('Error:', error);
} finally {
hideLoading(logoutBtn, originalText);
}
});
// Get all users
getAllUsersBtn.addEventListener('click', async () => {
const originalText = showLoading(getAllUsersBtn);
try {
const response = await fetch(`${API_BASE_URL}users?page=1&limit=10`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
const data = await response.json();
displayResponse('Get All Users', data, response.status);
} catch (error) {
displayResponse('Error', {
error: error.message,
note: 'Không thể kết nối đến API. Hãy đảm bảo server đang chạy tại ' + API_BASE_URL
}, 500);
console.error('Error:', error);
} finally {
hideLoading(getAllUsersBtn, originalText);
}
});
// Test health endpoint
testHealthBtn.addEventListener('click', async () => {
const originalText = showLoading(testHealthBtn);
try {
const response = await fetch(`/health`, {
method: 'GET',
});
const data = await response.json();
displayResponse('Health Check', data, response.status);
} catch (error) {
displayResponse('Error', {
error: error.message,
note: 'Không thể kết nối đến API. Hãy đảm bảo server đang chạy tại ' + API_BASE_URL
}, 500);
console.error('Error:', error);
} finally {
hideLoading(testHealthBtn, originalText);
}
});
// Auto-test health on page load
window.addEventListener('load', () => {
setTimeout(() => {
testHealthBtn.click();
}, 500);
});

290
public/login.html Normal file
View File

@@ -0,0 +1,290 @@
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sena DB API - Login Test</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 20px;
}
.container {
background: white;
border-radius: 10px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
overflow: hidden;
max-width: 1000px;
width: 100%;
display: grid;
grid-template-columns: 1fr 1fr;
}
.login-section {
padding: 50px 40px;
}
.response-section {
background: #f8f9fa;
padding: 50px 40px;
border-left: 1px solid #e0e0e0;
overflow-y: auto;
max-height: 600px;
}
h2 {
color: #333;
margin-bottom: 10px;
}
.subtitle {
color: #666;
margin-bottom: 30px;
font-size: 14px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
color: #555;
margin-bottom: 8px;
font-weight: 500;
font-size: 14px;
}
input {
width: 100%;
padding: 12px 15px;
border: 2px solid #e0e0e0;
border-radius: 6px;
font-size: 14px;
transition: border-color 0.3s;
}
input:focus {
outline: none;
border-color: #667eea;
}
.btn {
width: 100%;
padding: 14px;
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;
}
.btn:hover {
transform: translateY(-2px);
}
.btn:active {
transform: translateY(0);
}
.btn:disabled {
background: #ccc;
cursor: not-allowed;
transform: none;
}
.btn-secondary {
background: #6c757d;
margin-top: 5px;
}
.btn-secondary:hover {
background: #5a6268;
}
.response-content {
background: white;
border-radius: 6px;
padding: 15px;
margin-bottom: 15px;
border: 1px solid #e0e0e0;
}
.response-title {
font-weight: 600;
color: #333;
margin-bottom: 10px;
font-size: 14px;
}
pre {
background: #2d2d2d;
color: #f8f8f2;
padding: 15px;
border-radius: 4px;
overflow-x: auto;
font-size: 13px;
line-height: 1.5;
}
.status {
display: inline-block;
padding: 4px 12px;
border-radius: 4px;
font-size: 12px;
font-weight: 600;
margin-bottom: 10px;
}
.status.success {
background: #d4edda;
color: #155724;
}
.status.error {
background: #f8d7da;
color: #721c24;
}
.endpoint-info {
background: #e7f3ff;
padding: 15px;
border-radius: 6px;
margin-bottom: 20px;
font-size: 13px;
color: #004085;
}
.endpoint-info code {
background: white;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Courier New', monospace;
}
@media (max-width: 768px) {
.container {
grid-template-columns: 1fr;
}
.response-section {
border-left: none;
border-top: 1px solid #e0e0e0;
}
}
.example-data {
background: #fff3cd;
padding: 10px;
border-radius: 4px;
margin-bottom: 15px;
font-size: 12px;
color: #856404;
}
.loading {
display: inline-block;
width: 16px;
height: 16px;
border: 2px solid #ffffff;
border-top-color: transparent;
border-radius: 50%;
animation: spin 0.6s linear infinite;
vertical-align: middle;
margin-left: 8px;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="container">
<!-- Login Section -->
<div class="login-section">
<h2>🔐 Login Test</h2>
<p class="subtitle">Test quy trình đăng nhập API</p>
<div class="endpoint-info">
<strong>Endpoints:</strong><br>
POST <code>/api/auth/login</code> - Đăng nhập<br>
POST <code>/api/auth/register</code> - Đăng ký<br>
POST <code>/api/auth/verify-token</code> - Verify token<br>
POST <code>/api/auth/logout</code> - Đăng xuất<br>
GET <code>/api/users</code> - Danh sách users
</div>
<div class="example-data">
<strong>✅ API sẵn sàng!</strong> Hệ thống đã có đầy đủ các endpoint: login, register, verify-token, logout.
</div>
<form id="loginForm">
<div class="form-group">
<label for="username">Username hoặc Email:</label>
<input type="text" id="username" name="username" placeholder="Nhập username hoặc email" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Nhập mật khẩu" required>
</div>
<button type="submit" class="btn" id="loginBtn">
Đăng nhập
</button>
</form>
<button class="btn btn-secondary" id="registerBtn">
Đăng ký tài khoản mới
</button>
<button class="btn btn-secondary" id="verifyTokenBtn">
Verify Token
</button>
<button class="btn btn-secondary" id="logoutBtn">
Đăng xuất
</button>
<button class="btn btn-secondary" id="getAllUsersBtn">
Lấy danh sách Users
</button>
<button class="btn btn-secondary" id="testHealthBtn">
Test Health Endpoint
</button>
</div>
<!-- Response Section -->
<div class="response-section">
<h2>📊 Kết quả</h2>
<p class="subtitle">Response từ API server</p>
<div id="responseContainer">
<div class="response-content">
<p style="color: #666; text-align: center;">Chưa có request nào được gửi</p>
</div>
</div>
</div>
</div>
<script src="/js/login.js"></script>
</body>
</html>