|
228 | 228 | } |
229 | 229 |
|
230 | 230 | const response = await spotifyFetch(`https://api.spotify.com/v1/playlists/${playlistId}/tracks`); |
231 | | - if (!response) return []; // Handle case where auth refresh was triggered |
| 231 | + if (!response) return []; |
232 | 232 |
|
233 | 233 | const data = await response.json(); |
234 | 234 | return data.items |
@@ -387,33 +387,27 @@ <h3 class="title">${track.name}</h3> |
387 | 387 | } |
388 | 388 |
|
389 | 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 | | - |
395 | 390 | const response = await fetch(url, { |
396 | 391 | ...options, |
397 | 392 | headers: { |
398 | | - 'Authorization': `Bearer ${token}`, |
| 393 | + 'Authorization': `Bearer ${localStorage.getItem('spotify_access_token')}`, |
399 | 394 | ...options.headers |
400 | 395 | } |
401 | 396 | }); |
402 | 397 |
|
403 | 398 | if (response.status === 401) { |
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 | | - } |
413 | | - refreshAuth(); |
| 399 | + localStorage.clear(); |
| 400 | + sessionStorage.clear(); |
| 401 | + const clientId = localStorage.getItem('spotify_client_id'); |
| 402 | + const scope = 'streaming user-read-email user-read-private user-modify-playback-state playlist-read-private'; |
| 403 | + window.location.href = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}`; |
414 | 404 | return; |
415 | 405 | } |
416 | 406 |
|
| 407 | + if (!response.ok) { |
| 408 | + throw new Error(`API request failed with status ${response.status}`); |
| 409 | + } |
| 410 | + |
417 | 411 | return response; |
418 | 412 | } |
419 | 413 | </script> |
|
0 commit comments