Skip to content

Commit

Permalink
Merge pull request #450 from vishalvivekm/cookie-d
Browse files Browse the repository at this point in the history
only look for user details if a cookie exists and hasn't expired
  • Loading branch information
vishalvivekm authored Mar 3, 2025
2 parents c9ce12e + 2d427a6 commit cb8454b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions layouts/partials/navbar.html
Original file line number Diff line number Diff line change
@@ -198,6 +198,8 @@
}

let isUserAuthenticated = false;
let expiredToken = "";

function removeCookie(cookieName) {
document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}
@@ -216,12 +218,12 @@
const checkUserAuth = async () => {
try {
const token = getCookieValue("provider_token");
if (!token) { // cookie doesn't exist, user logged out of cloud
if (!token || token === expiredToken) { // cookie doesn't exist or has expired (due to user logout)
if (isUserAuthenticated) {
showSignInButton();
isUserAuthenticated = false;
}
throw new Error("no cookie to authenticate");
throw new Error("missing or expired cookie");
}
const re = await fetch("https://cloud.layer5.io/api/identity/users/profile", {
method: 'GET',
@@ -230,6 +232,10 @@
},
});

if (res.status === 401) { // cookie has expired
expiredToken = token;
throw new Error("unauthorized");
}
if (re.status !== 200) {
throw new Error(`HTTP error! status: ${response.status}`);
}

0 comments on commit cb8454b

Please sign in to comment.