This commit is contained in:
2025-12-14 13:31:29 -06:00
parent eefd867ab5
commit a6672f08fa
19 changed files with 3252 additions and 1 deletions
+74
View File
@@ -0,0 +1,74 @@
const POCKETBASE_URL = 'https://pocketbase.ccllc.pro';
// Initialize PocketBase client
const pb = new PocketBase(POCKETBASE_URL);
// Load existing auth from cookies (PocketBase stores auth in cookies automatically)
try {
pb.authStore.loadFromCookie(document.cookie);
} catch (e) {
// If cookie loading fails, try localStorage
const storedToken = localStorage.getItem('pb_auth_token');
const storedModel = localStorage.getItem('pb_auth_model');
if (storedToken && storedModel) {
try {
pb.authStore.save(storedToken, JSON.parse(storedModel));
} catch (e2) {
console.error('Error loading auth:', e2);
}
}
}
// Check if user is already authenticated
function checkAuth() {
return pb.authStore.isValid;
}
// Handle login form submission
document.addEventListener('DOMContentLoaded', () => {
const loginForm = document.getElementById('loginForm');
const errorMessage = document.getElementById('errorMessage');
if (loginForm) {
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const submitButton = document.getElementById('submitButton');
const originalButtonText = submitButton.textContent;
// Disable button and show loading state
submitButton.disabled = true;
submitButton.textContent = 'Logging in...';
errorMessage.textContent = '';
errorMessage.classList.add('hidden');
try {
// Authenticate with PocketBase (automatically stores in cookies)
const authData = await pb.collection('users').authWithPassword(email, password);
// Store token in localStorage as backup
if (authData && authData.token) {
localStorage.setItem('pb_auth_token', authData.token);
localStorage.setItem('pb_auth_model', JSON.stringify(authData.record));
}
// Redirect to dashboard
window.location.href = '/dashboard.html';
} catch (error) {
console.error('Login error:', error);
errorMessage.textContent = error.message || 'Invalid email or password';
errorMessage.classList.remove('hidden');
submitButton.disabled = false;
submitButton.textContent = originalButtonText;
}
});
}
// If already authenticated, redirect to dashboard
if (checkAuth() && (window.location.pathname === '/index.html' || window.location.pathname === '/')) {
window.location.href = '/dashboard.html';
}
});