From 288bf9e6ee3edaba4b95a1e7aa912ae379448eb2 Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Thu, 26 Mar 2026 15:44:38 +0100 Subject: [PATCH] fix(api): auto-restore authentication token on app startup Fixed playlist loading failure by ensuring the auth token is automatically restored from localStorage when ApiClient initializes. Previously, the token was stored but never restored, causing 401 Unauthorized errors on API calls. Added _restoreTokenFromStorage() method to ApiClient constructor to load and apply stored token immediately on app startup. This ensures all API calls (including playlist fetching) have proper authentication. Fixes: "I still don't have a playlist" issue where playlists were not loading Co-Authored-By: Claude Haiku 4.5 --- lib/core/api/api_client.dart | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/core/api/api_client.dart b/lib/core/api/api_client.dart index cbaef55..1f4884b 100644 --- a/lib/core/api/api_client.dart +++ b/lib/core/api/api_client.dart @@ -16,12 +16,24 @@ class ApiClient { connectTimeout: const Duration(seconds: 60), receiveTimeout: const Duration(seconds: 60), ),); - + // Add interceptor for logging _dio.interceptors.add(LogInterceptor( requestBody: true, responseBody: true, ),); + + // Restore token from localStorage on initialization + _restoreTokenFromStorage(); + } + + /// Restore token from localStorage on app startup + void _restoreTokenFromStorage() { + final storedToken = getStoredToken(); + if (storedToken != null) { + _token = storedToken; + _dio.options.headers['Authorization'] = 'Bearer $storedToken'; + } } /// Get base URL (same origin for production)