getAccessToken method
Gets a valid access token, automatically handling refresh This should be used for all external API calls
Returns:
- Valid access token if available or successfully refreshed
- null if user needs to authenticate (refresh token invalid/expired)
Note: This method does NOT require BuildContext as it only handles token refresh, not UI navigation. If null is returned, the calling app should handle sign-in.
Implementation
Future<String?> getAccessToken() async {
final accessToken = await TokenStorage.getAccessToken();
// Check if we have an access token
if (accessToken == null || accessToken.isEmpty) {
debugPrint('[INFO] No access token found, checking refresh token');
return await _handleTokenRefreshOnly();
}
// Validate JWT token expiry
if (_isTokenExpired(accessToken)) {
debugPrint('[INFO] Access token expired, attempting refresh');
return await _handleTokenRefreshOnly();
}
debugPrint('[INFO] Access token is valid');
return accessToken;
}