Skip to content

Commit cb8454b

Browse files
author
Vivek Vishal
authored
Merge pull request #450 from vishalvivekm/cookie-d
only look for user details if a cookie exists and hasn't expired
2 parents c9ce12e + 2d427a6 commit cb8454b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

layouts/partials/navbar.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@
198198
}
199199

200200
let isUserAuthenticated = false;
201+
let expiredToken = "";
202+
201203
function removeCookie(cookieName) {
202204
document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
203205
}
@@ -216,12 +218,12 @@
216218
const checkUserAuth = async () => {
217219
try {
218220
const token = getCookieValue("provider_token");
219-
if (!token) { // cookie doesn't exist, user logged out of cloud
221+
if (!token || token === expiredToken) { // cookie doesn't exist or has expired (due to user logout)
220222
if (isUserAuthenticated) {
221223
showSignInButton();
222224
isUserAuthenticated = false;
223225
}
224-
throw new Error("no cookie to authenticate");
226+
throw new Error("missing or expired cookie");
225227
}
226228
const re = await fetch("https://cloud.layer5.io/api/identity/users/profile", {
227229
method: 'GET',
@@ -230,6 +232,10 @@
230232
},
231233
});
232234

235+
if (res.status === 401) { // cookie has expired
236+
expiredToken = token;
237+
throw new Error("unauthorized");
238+
}
233239
if (re.status !== 200) {
234240
throw new Error(`HTTP error! status: ${response.status}`);
235241
}

0 commit comments

Comments
 (0)