Skip to content

Commit 77b7db8

Browse files
authored
Merge pull request #1113 from layer5io/copilot/fix-issue-1112
fix(navbar): recognize authenticated users with active sessions
2 parents f26ce8b + 86fe88c commit 77b7db8

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

layouts/partials/navbar.html

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -291,23 +291,29 @@
291291

292292
const checkUserAuth = async () => {
293293
try {
294-
const token = getCookieValue("provider_token");
295-
if (!token || token === expiredToken) { // cookie doesn't exist or has expired (due to user logout)
296-
if (isUserAuthenticated) {
297-
showSignInButton();
298-
isUserAuthenticated = false;
299-
}
300-
throw new Error("missing or expired cookie");
301-
}
302-
const re = await fetch(`${cloudAppUrl}/api/identity/users/profile`, {
294+
const token = getCookieValue("provider_token");
295+
296+
if (token && token === expiredToken) {
297+
throw new Error("expired cookie");
298+
}
299+
300+
const fetchOptions = {
303301
method: 'GET',
304-
headers: {
302+
credentials: 'include',
303+
};
304+
305+
if (token) {
306+
fetchOptions.headers = {
305307
'Authorization': `Bearer ${token}`,
306-
},
307-
});
308+
};
309+
}
310+
311+
const re = await fetch(`${cloudAppUrl}/api/identity/users/profile`, fetchOptions);
308312

309-
if (re.status === 401) { // cookie has expired
310-
expiredToken = token;
313+
if (re.status === 401) {
314+
if (token) {
315+
expiredToken = token;
316+
}
311317
throw new Error("unauthorized");
312318
}
313319
if (re.status !== 200) {
@@ -318,11 +324,13 @@
318324
updateUI(response);
319325

320326
} catch (error) {
321-
// console.error("could not set user details.", error);
322327
showSignInButton();
328+
if (isUserAuthenticated) {
329+
isUserAuthenticated = false;
330+
}
323331
}
324332
};
325-
function getAvatarUrl(response) {
333+
function getAvatarUrl(response) {
326334
const avatarUrl = response?.avatarUrl;
327335

328336
return (typeof avatarUrl === 'string' && avatarUrl.trim()) || '';

0 commit comments

Comments
 (0)