Spaces:
Sleeping
Sleeping
File size: 2,935 Bytes
50c1c55 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | // admin.js - Admin Panel JavaScript
const loginSection = document.getElementById('login-section');
const createSection = document.getElementById('create-section');
const loginForm = document.getElementById('login-form');
const createForm = document.getElementById('create-form');
// Check if already logged in
if (localStorage.getItem('admin_token')) {
loginSection.style.display = 'none';
createSection.style.display = 'block';
}
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const password = document.getElementById('password').value;
try {
const response = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password })
});
const data = await response.json();
if (data.success) {
localStorage.setItem('admin_token', data.token);
loginSection.style.display = 'none';
createSection.style.display = 'block';
showAlert('success', 'Login successful!');
} else {
showAlert('error', 'Invalid password');
}
} catch (error) {
showAlert('error', 'Login failed');
}
});
createForm.addEventListener('submit', async (e) => {
e.preventDefault();
const title = document.getElementById('title').value;
const redirect = document.getElementById('redirect').value;
const name = document.getElementById('name').value;
const verify = document.getElementById('verify').checked;
const token = localStorage.getItem('admin_token');
try {
const response = await fetch('/api/create', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ title, redirect, name, verify })
});
const data = await response.json();
if (data.success) {
showAlert('success', 'Short link created successfully!');
createForm.reset();
setTimeout(() => {
window.location.href = '/home';
}, 2000);
} else {
showAlert('error', 'Failed to create link');
}
} catch (error) {
showAlert('error', 'Failed to create link');
}
});
function logout() {
localStorage.removeItem('admin_token');
window.location.href = '/home';
}
function showAlert(type, message) {
const container = document.getElementById('alert-container');
const alert = document.createElement('div');
alert.className = `alert alert-${type}`;
let icon = 'fa-check-circle';
if (type === 'error') icon = 'fa-exclamation-circle';
else if (type === 'info') icon = 'fa-info-circle';
alert.innerHTML = `
<span class="alert-icon"><i class="fas ${icon}"></i></span>
<span>${message}</span>
<button class="alert-close" onclick="this.parentElement.remove()">
<i class="fas fa-times"></i>
</button>
`;
container.appendChild(alert);
setTimeout(() => alert.remove(), 5000);
} |