|
211 | 211 | const [key, value] = param.split('='); |
212 | 212 | hashParams[key] = decodeURIComponent(value); |
213 | 213 | }); |
| 214 | + |
| 215 | + // If we got a new token, save it immediately |
| 216 | + if (hashParams.access_token) { |
| 217 | + console.log('Got new token from hash:', hashParams.access_token.substring(0, 10) + '...'); |
| 218 | + localStorage.setItem('spotify_access_token', hashParams.access_token); |
| 219 | + } |
| 220 | + |
214 | 221 | return hashParams; |
215 | 222 | } |
216 | 223 |
|
@@ -380,24 +387,33 @@ <h3 class="title">${track.name}</h3> |
380 | 387 | } |
381 | 388 |
|
382 | 389 | async function spotifyFetch(url, options = {}) { |
| 390 | + console.log('Making request to:', url); |
| 391 | + |
| 392 | + const token = localStorage.getItem('spotify_access_token'); |
| 393 | + console.log('Using token:', token?.substring(0, 10) + '...'); |
| 394 | + |
383 | 395 | const response = await fetch(url, { |
384 | 396 | ...options, |
385 | 397 | headers: { |
386 | | - 'Authorization': `Bearer ${localStorage.getItem('spotify_access_token')}`, |
| 398 | + 'Authorization': `Bearer ${token}`, |
387 | 399 | ...options.headers |
388 | 400 | } |
389 | 401 | }); |
390 | 402 |
|
391 | 403 | if (response.status === 401) { |
392 | | - // Token is invalid, refresh authentication |
| 404 | + console.log('Got 401 error for URL:', url); |
| 405 | + console.log('Full response:', response); |
| 406 | + // Let's check if we just got redirected back from auth |
| 407 | + const hashParams = getHashParams(); |
| 408 | + if (hashParams.access_token) { |
| 409 | + console.log('We already have a new token but still getting 401s'); |
| 410 | + // Something else is wrong - maybe token isn't being stored properly |
| 411 | + return response; |
| 412 | + } |
393 | 413 | refreshAuth(); |
394 | 414 | return; |
395 | 415 | } |
396 | 416 |
|
397 | | - if (!response.ok) { |
398 | | - throw new Error(`API request failed with status ${response.status}`); |
399 | | - } |
400 | | - |
401 | 417 | return response; |
402 | 418 | } |
403 | 419 | </script> |
|
0 commit comments