sleekheader-supreme / script.js
matheusbezerra1's picture
ok, how to access the admin page?
49dafe6 verified
Raw History Blame Contribute Delete
4.76 kB
// Shared JavaScript
const API_BASE_URL = 'http://localhost:5000/api';
// Check admin access
function checkAdminAccess() {
const token = localStorage.getItem('authToken');
if (!token) {
window.location.href = '/login.html';
return false;
}
// Demo admin access - in production, verify with backend
function isAdminUser() {
// For demo purposes, allow access if token exists
// In a real app, you would verify the token and user role with the backend
return true;
}
// Handle logout
function handleLogout() {
localStorage.removeItem('authToken');
window.location.href = '/login.html';
}
document.addEventListener('DOMContentLoaded', () => {
// Form submission handling
const forms = document.querySelectorAll('form');
forms.forEach(form => {
form.addEventListener('submit', (e) => {
e.preventDefault();
// Form handling logic would go here
console.log('Form submitted');
});
});
// Property loading for properties page
if (window.location.pathname.includes('properties.html')) {
loadProperties();
}
});
async function loadProperties() {
try {
const response = await fetch('http://localhost:5000/api/properties');
const properties = await response.json();
const propertiesContainer = document.querySelector('.grid');
if (propertiesContainer) {
propertiesContainer.innerHTML = properties.map(property => `
<div class="property-card bg-white rounded-lg overflow-hidden shadow-lg transition-all duration-300 hover:shadow-xl">
<img src="${property.image}" alt="${property.title}" class="w-full h-64 object-cover">
<div class="p-6">
<h3 class="text-xl font-bold text-gray-800">${property.title}</h3>
<p class="mt-2 text-gray-600">${property.location}</p>
<div class="mt-4 flex justify-between items-center">
<span class="text-lg font-semibold text-blue-600">${property.price}</span>
<span class="text-sm text-gray-500">${property.size}</span>
</div>
${property.features ? `<div class="mt-4 flex space-x-2">
${property.features.map(feat =>
`<span class="bg-gray-100 px-2 py-1 rounded text-sm">${feat}</span>`
).join('')}
</div>` : ''}
</div>
</div>
`).join('');
}
} catch (error) {
console.error('Error loading properties:', error);
alert('Failed to load properties. Please try again later.');
}
}
// Handle login form submission
document.addEventListener('DOMContentLoaded', () => {
const loginForm = document.getElementById('loginForm');
if (loginForm) {
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const email = e.target.email.value;
const password = e.target.password.value;
try {
const response = await fetch('http://localhost:5000/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
const data = await response.json();
if (data.token) {
localStorage.setItem('authToken', data.token);
// Check if admin and redirect accordingly
if (email === 'admin@luxuryestates.com' || data.user?.role === 'admin') {
window.location.href = '/admin.html';
} else {
window.location.href = '/properties.html';
}
} catch (error) {
console.error('Login error:', error);
// For demo purposes, allow admin access with specific credentials
if (email === 'admin@luxuryestates.com' && password === 'admin123') {
localStorage.setItem('authToken', 'demo-admin-token');
window.location.href = '/admin.html';
}
});
}
// Form submission handling for contact form
const contactForm = document.querySelector('form');
if (contactForm && !contactForm.id) {
contactForm.addEventListener('submit', (e) => {
e.preventDefault();
// In a real app, you'd send this to your backend
alert('Message sent! We will contact you soon.');
contactForm.reset();
});
}
});