specialized-agents / static /js /check-auth.js
pvanand's picture
Create js/check-auth.js
bec0504 verified
raw
history blame
1.47 kB
const client = new Appwrite.Client();
client.setEndpoint('https://cloud.appwrite.io/v1')
.setProject('66c2f6c7000abed7f1f9');
const account = new Appwrite.Account(client);
async function checkAuth() {
try {
const session = await account.getSession('current');
return session;
} catch (error) {
console.error("Not authenticated", error);
return null;
}
}
function redirectToLogin() {
window.location.href = '/';
}
async function logout() {
// Show loading indicator
const loadingElement = document.createElement('div');
loadingElement.textContent = 'Logging out...';
document.body.appendChild(loadingElement);
try {
// Set a timeout for the logout process
const logoutPromise = account.deleteSession('current');
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Logout timed out')), 5000) // 5 second timeout
);
await Promise.race([logoutPromise, timeoutPromise]);
// Perform any client-side cleanup here
// For example, clear any local storage items
localStorage.clear();
// Redirect to login page
window.location.href = '/';
} catch (error) {
console.error("Logout failed:", error);
alert("Logout failed. Please try again.");
} finally {
// Remove loading indicator
document.body.removeChild(loadingElement);
}
}