|
291 | 291 |
|
292 | 292 | const checkUserAuth = async () => { |
293 | 293 | 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 = { |
303 | 301 | method: 'GET', |
304 | | - headers: { |
| 302 | + credentials: 'include', |
| 303 | + }; |
| 304 | + |
| 305 | + if (token) { |
| 306 | + fetchOptions.headers = { |
305 | 307 | 'Authorization': `Bearer ${token}`, |
306 | | - }, |
307 | | - }); |
| 308 | + }; |
| 309 | + } |
| 310 | + |
| 311 | + const re = await fetch(`${cloudAppUrl}/api/identity/users/profile`, fetchOptions); |
308 | 312 |
|
309 | | - if (re.status === 401) { // cookie has expired |
310 | | - expiredToken = token; |
| 313 | + if (re.status === 401) { |
| 314 | + if (token) { |
| 315 | + expiredToken = token; |
| 316 | + } |
311 | 317 | throw new Error("unauthorized"); |
312 | 318 | } |
313 | 319 | if (re.status !== 200) { |
|
318 | 324 | updateUI(response); |
319 | 325 |
|
320 | 326 | } catch (error) { |
321 | | - // console.error("could not set user details.", error); |
322 | 327 | showSignInButton(); |
| 328 | + if (isUserAuthenticated) { |
| 329 | + isUserAuthenticated = false; |
| 330 | + } |
323 | 331 | } |
324 | 332 | }; |
325 | | - function getAvatarUrl(response) { |
| 333 | + function getAvatarUrl(response) { |
326 | 334 | const avatarUrl = response?.avatarUrl; |
327 | 335 |
|
328 | 336 | return (typeof avatarUrl === 'string' && avatarUrl.trim()) || ''; |
|
0 commit comments