42 lines
829 B
JavaScript
42 lines
829 B
JavaScript
// Authentication functions
|
|
|
|
/**
|
|
* Login with email and password
|
|
*/
|
|
async function login(email, password) {
|
|
try {
|
|
const authData = await pb.collection('users').authWithPassword(email, password);
|
|
return authData;
|
|
} catch (error) {
|
|
console.error('Login error:', error);
|
|
throw new Error(error.message || 'Login failed. Please check your credentials.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Logout current user
|
|
*/
|
|
function logout() {
|
|
pb.authStore.clear();
|
|
window.location.href = 'index.html';
|
|
}
|
|
|
|
/**
|
|
* Check if user is authenticated, redirect to login if not
|
|
*/
|
|
function checkAuth() {
|
|
if (!pb.authStore.isValid) {
|
|
window.location.href = 'index.html';
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get current user
|
|
*/
|
|
function getCurrentUser() {
|
|
return pb.authStore.model;
|
|
}
|
|
|