Added more frontend and some more login logic
This commit is contained in:
109
frontend/assets/js/auth.js
Normal file
109
frontend/assets/js/auth.js
Normal file
@@ -0,0 +1,109 @@
|
||||
// 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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (!accessToken && !refreshToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
async function tryTokenRefresh() {
|
||||
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);
|
||||
}
|
||||
|
||||
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;";
|
||||
return false;
|
||||
}
|
||||
|
||||
async function checkAuth() {
|
||||
console.log("Auth check started...");
|
||||
console.log("AccessToken present:", !!accessToken);
|
||||
console.log("RefreshToken present:", !!refreshToken);
|
||||
|
||||
if (!cookieAccessToken && accessToken) {
|
||||
console.log("Access token cookie missing, but present in localStorage. Forcing refresh...");
|
||||
} else 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. 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();
|
||||
|
||||
if (refreshSuccessful) {
|
||||
console.log("Refresh successful! Redirecting to dashboard...");
|
||||
window.location.href = "/dashboard";
|
||||
return;
|
||||
} else {
|
||||
console.log("Refresh failed. Staying on login.");
|
||||
}
|
||||
} else {
|
||||
console.log("No refresh token present. User must log in normally.");
|
||||
}
|
||||
|
||||
console.log("Authentication completely failed. Clearing remnants...");
|
||||
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;";
|
||||
}
|
||||
|
||||
checkAuth();
|
||||
})();
|
||||
Reference in New Issue
Block a user