Skip to content

Fix: API requests fail with 401 Unauthorized after token expires - #40

Open
szeredniklaszlo wants to merge 1 commit into
sonic-liberation:mainfrom
szeredniklaszlo:main
Open

Fix: API requests fail with 401 Unauthorized after token expires#40
szeredniklaszlo wants to merge 1 commit into
sonic-liberation:mainfrom
szeredniklaszlo:main

Conversation

@szeredniklaszlo

Copy link
Copy Markdown

Title: Fix: API requests fail with 401 Unauthorized after token expires

Summary

This pull request introduces an automatic token refresh mechanism to fix a critical authentication bug. Currently, when a Spotify access token expires, all subsequent API requests fail with a 401 Unauthorized error, forcing the user to manually log out and log back in.

This is especially noticeable when opening Spotify links from external applications (e.g., https://open.spotify.com/track/...) using the method mentioned below, as the app opens but fails to load the track data, showing a placeholder screen.

The Problem

The plugin's API endpoints (for tracks, artists, etc.) did not handle the 401 Unauthorized error. When the token expired, they would throw an exception instead of triggering the existing refreshCredentials() function in the auth module.

The Solution

The fix is architecturally centralized, ensuring all API calls are resilient to token expiry:

  1. Centralized Error Handling (SafeCaller utility): A new utility class, SafeCaller, has been created. It wraps every API call in a function that uses Hetu's .then(catchError:) pattern. If a 401 error is detected, it automatically calls auth.refreshCredentials() and retries the original request with the new, valid token.

  2. Dependency Injection: The main plugin.ht now injects the auth module into every endpoint, making the authentication context available wherever needed.

  3. Refactored Endpoints: All API-calling functions have been updated to use the SafeCaller, making the retry logic consistent across the entire plugin.

This ensures a seamless user experience, as token refreshes happen automatically in the background without interrupting the user.

Context for External Link Handling

This fix is crucial for users who rely on opening Spotify links via third-party tools (like URLCheck on Android) that map https://open.spotify.com/... URLs to Spotube's internal deep links (e.g., spotube://spotify/track/TRACK_ID). Without this patch, this core functionality becomes unreliable as soon as the session token expires.

This PR makes that workflow robust and reliable again.

This may be related to these:
KRTirtho/spotube#2818
#36
#27

Introduce SafeCaller to handle API request retries on authorization errors.
Integrate SafeCaller across all API endpoint segments for robust error handling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant