mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 05:30:13 +01:00
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
// /js/auth.ts
|
|
|
|
function deleteCookie(name: string): void {
|
|
document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
|
}
|
|
|
|
async function logoutUser(): Promise<void> {
|
|
await fetch("/api/auth/logout", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
deleteCookie('token');
|
|
|
|
window.location.href = "/index.html";
|
|
}
|
|
|
|
function redirectToLogin(): void {
|
|
window.location.href = 'login.html';
|
|
}
|
|
|
|
function checkAuth(): boolean {
|
|
// Basic auth check via presence of token cookie
|
|
return document.cookie.includes('token=');
|
|
}
|
|
|
|
function setupAuthUI(): void {
|
|
const joinNowBtn = document.getElementById('joinnow-btn');
|
|
const signInBtn = document.getElementById('signin-btn');
|
|
const logoutBtn = document.getElementById('logout-btn');
|
|
|
|
const isAuthenticated = checkAuth();
|
|
|
|
if (joinNowBtn) {
|
|
joinNowBtn.classList.toggle('d-none', isAuthenticated);
|
|
joinNowBtn.addEventListener('click', redirectToLogin);
|
|
}
|
|
|
|
if (signInBtn) {
|
|
signInBtn.classList.toggle('d-none', isAuthenticated);
|
|
signInBtn.addEventListener('click', redirectToLogin);
|
|
}
|
|
|
|
if (logoutBtn) {
|
|
logoutBtn.classList.toggle('d-none', !isAuthenticated);
|
|
logoutBtn.addEventListener('click', (e) => {
|
|
e.preventDefault();
|
|
logoutUser();
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
// Initialize on load
|
|
document.addEventListener('DOMContentLoaded', setupAuthUI);
|