110 lines
3.9 KiB
JavaScript
110 lines
3.9 KiB
JavaScript
// auth.js
|
|
(() => {
|
|
const currentPath = window.location.pathname;
|
|
|
|
if (currentPath !== "/" && currentPath !== "/login" && currentPath !== "/register") {
|
|
return;
|
|
}
|
|
|
|
function getCookie(name) {
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; ${name}=`);
|
|
if (parts.length === 2) return parts.pop().split(';').shift();
|
|
return null;
|
|
}
|
|
|
|
function clearAllAuth() {
|
|
console.log("Clearing all auth remnants from cookies and localStorage...");
|
|
localStorage.removeItem("access_token");
|
|
localStorage.removeItem("refresh_token");
|
|
document.cookie = "access_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
|
|
document.cookie = "refresh_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
|
|
}
|
|
|
|
async function tryTokenRefresh(refreshToken) {
|
|
if (!refreshToken) return false;
|
|
|
|
try {
|
|
const response = await fetch("/api/refresh", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ refresh_token: refreshToken })
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
|
|
localStorage.setItem("access_token", data.access_token);
|
|
localStorage.setItem("refresh_token", data.refresh_token);
|
|
|
|
document.cookie = `access_token=${data.access_token}; path=/; max-age=900; SameSite=Lax; Secure`;
|
|
document.cookie = `refresh_token=${data.refresh_token}; path=/; max-age=604800; SameSite=Lax; Secure`;
|
|
|
|
return true;
|
|
}
|
|
} catch (err) {
|
|
console.error("Refresh request failed:", err);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
async function checkAuth() {
|
|
const cookieAccessToken = getCookie("access_token");
|
|
const cookieRefreshToken = getCookie("refresh_token");
|
|
const localAccessToken = localStorage.getItem("access_token");
|
|
const localRefreshToken = localStorage.getItem("refresh_token");
|
|
|
|
const accessToken = cookieAccessToken || localAccessToken;
|
|
const refreshToken = cookieRefreshToken || localRefreshToken;
|
|
|
|
console.log("Auth check started...");
|
|
console.log("AccessToken present:", !!accessToken);
|
|
console.log("RefreshToken present:", !!refreshToken);
|
|
|
|
if (!accessToken && !refreshToken) {
|
|
console.log("No tokens found. User is guest.");
|
|
return;
|
|
}
|
|
|
|
if (accessToken) {
|
|
try {
|
|
console.log("Attempting ping with access token...");
|
|
const response = await fetch("/api/ping", {
|
|
method: "GET",
|
|
headers: { "Authorization": `Bearer ${accessToken}` }
|
|
});
|
|
|
|
if (response.ok) {
|
|
console.log("Ping successful! Redirecting to dashboard...");
|
|
window.location.href = "/dashboard";
|
|
return;
|
|
} else {
|
|
console.log("Ping failed. Token might be expired. Status:", response.status);
|
|
}
|
|
} catch (err) {
|
|
console.error("Network error during ping:", err);
|
|
}
|
|
}
|
|
|
|
if (refreshToken) {
|
|
console.log("Starting token refresh to rebuild cookies...");
|
|
const refreshSuccessful = await tryTokenRefresh(refreshToken);
|
|
|
|
if (refreshSuccessful) {
|
|
console.log("Refresh successful! Redirecting to dashboard...");
|
|
window.location.href = "/dashboard";
|
|
return;
|
|
} else {
|
|
console.log("Refresh failed. Refresh token is invalid/expired.");
|
|
}
|
|
} else {
|
|
console.log("No refresh token present.");
|
|
}
|
|
|
|
clearAllAuth();
|
|
console.log("Authentication completely failed. Staying on current guest page.");
|
|
}
|
|
|
|
checkAuth();
|
|
})(); |