last_edit / frontend /test-api.html
Alinabil1's picture
add: API connection test page for debugging
510ced6
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API Test</title>
<style>
body { font-family: Arial; padding: 20px; background: #1a1a1a; color: #fff; }
button { padding: 10px 20px; margin: 10px; background: #c8f04e; border: none; cursor: pointer; }
pre { background: #2a2a2a; padding: 15px; border-radius: 8px; overflow-x: auto; }
.success { color: #4ade80; }
.error { color: #ff4d4d; }
</style>
</head>
<body>
<h1>πŸ§ͺ API Connection Test</h1>
<div>
<h3>Current URL Info:</h3>
<pre id="urlInfo"></pre>
</div>
<button onclick="testHealth()">Test Health Endpoint</button>
<button onclick="testRegister()">Test Register</button>
<button onclick="testLogin()">Test Login</button>
<div>
<h3>Results:</h3>
<pre id="results"></pre>
</div>
<script>
const API = window.location.origin + '/api';
document.getElementById('urlInfo').textContent =
'Origin: ' + window.location.origin + '\n' +
'API URL: ' + API + '\n' +
'Full URL: ' + window.location.href;
function log(msg, isError = false) {
const el = document.getElementById('results');
const className = isError ? 'error' : 'success';
el.innerHTML += `<div class="${className}">${new Date().toLocaleTimeString()}: ${msg}</div>`;
}
async function testHealth() {
log('Testing /health endpoint...');
try {
const res = await fetch(API.replace('/api', '/health'));
const data = await res.json();
log('βœ… Health check: ' + JSON.stringify(data, null, 2));
} catch (err) {
log('❌ Health check failed: ' + err.message, true);
}
}
async function testRegister() {
log('Testing /api/users/register...');
const testEmail = 'test' + Date.now() + '@test.com';
try {
const res = await fetch(API + '/users/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: testEmail,
password: 'test123456'
})
});
const data = await res.json();
log('βœ… Register response: ' + JSON.stringify(data, null, 2));
} catch (err) {
log('❌ Register failed: ' + err.message, true);
}
}
async function testLogin() {
log('Testing /api/users/login...');
try {
const res = await fetch(API + '/users/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'admin@moharek.com',
password: 'admin123'
})
});
const data = await res.json();
log('βœ… Login response: ' + JSON.stringify(data, null, 2));
} catch (err) {
log('❌ Login failed: ' + err.message, true);
}
}
// Auto-test on load
setTimeout(() => {
log('πŸš€ Starting automatic tests...');
testHealth();
}, 500);
</script>
</body>
</html>