40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
// PocketBase auth bootstrap shared by pages
|
|
(function (global) {
|
|
const pb = new PocketBase('https://pocketbase.ccllc.pro');
|
|
|
|
function authHeaders() {
|
|
if (pb.authStore.isValid && pb.authStore.token) {
|
|
return { Authorization: `Bearer ${pb.authStore.token}` };
|
|
}
|
|
return {};
|
|
}
|
|
|
|
function getDisplayName() {
|
|
const model = pb.authStore.model || {};
|
|
return (
|
|
model.display_name ||
|
|
model.name ||
|
|
model.email ||
|
|
model.username ||
|
|
'Unknown'
|
|
);
|
|
}
|
|
|
|
function getEmail() {
|
|
const model = pb.authStore.model || {};
|
|
// Prefer email, but fall back to username if email missing
|
|
return model.email || model.username || null;
|
|
}
|
|
|
|
async function ensureAuth() {
|
|
if (pb.authStore.isValid) return true;
|
|
const path = new URL(window.location.href).pathname;
|
|
if (!path.endsWith('signin.html')) {
|
|
window.location.href = 'signin.html';
|
|
}
|
|
return false;
|
|
}
|
|
|
|
global.Auth = { pb, authHeaders, ensureAuth, getDisplayName, getEmail };
|
|
})(window);
|