Spaces:
Sleeping
Sleeping
Create js/check-auth.js
Browse files- static/js/check-auth.js +49 -0
static/js/check-auth.js
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const client = new Appwrite.Client();
|
2 |
+
client.setEndpoint('https://cloud.appwrite.io/v1')
|
3 |
+
.setProject('66c2f6c7000abed7f1f9');
|
4 |
+
|
5 |
+
const account = new Appwrite.Account(client);
|
6 |
+
|
7 |
+
async function checkAuth() {
|
8 |
+
try {
|
9 |
+
const session = await account.getSession('current');
|
10 |
+
return session;
|
11 |
+
} catch (error) {
|
12 |
+
console.error("Not authenticated", error);
|
13 |
+
return null;
|
14 |
+
}
|
15 |
+
}
|
16 |
+
|
17 |
+
function redirectToLogin() {
|
18 |
+
window.location.href = '/';
|
19 |
+
}
|
20 |
+
|
21 |
+
async function logout() {
|
22 |
+
// Show loading indicator
|
23 |
+
const loadingElement = document.createElement('div');
|
24 |
+
loadingElement.textContent = 'Logging out...';
|
25 |
+
document.body.appendChild(loadingElement);
|
26 |
+
|
27 |
+
try {
|
28 |
+
// Set a timeout for the logout process
|
29 |
+
const logoutPromise = account.deleteSession('current');
|
30 |
+
const timeoutPromise = new Promise((_, reject) =>
|
31 |
+
setTimeout(() => reject(new Error('Logout timed out')), 5000) // 5 second timeout
|
32 |
+
);
|
33 |
+
|
34 |
+
await Promise.race([logoutPromise, timeoutPromise]);
|
35 |
+
|
36 |
+
// Perform any client-side cleanup here
|
37 |
+
// For example, clear any local storage items
|
38 |
+
localStorage.clear();
|
39 |
+
|
40 |
+
// Redirect to login page
|
41 |
+
window.location.href = '/';
|
42 |
+
} catch (error) {
|
43 |
+
console.error("Logout failed:", error);
|
44 |
+
alert("Logout failed. Please try again.");
|
45 |
+
} finally {
|
46 |
+
// Remove loading indicator
|
47 |
+
document.body.removeChild(loadingElement);
|
48 |
+
}
|
49 |
+
}
|