I am working in a project where few APIs requires JWT token and few Access token, but in header they can accept both. so i am both of them. The APIs which requris JWT are working fine but those who require AccessToken are sending me 500 status code. I have double the token they are correct, and these APIs are working fine in postman too.
here is my network file code
class NetworkHelper { static const String _baseUrl = TarabutUrls.baseURL; static const Duration _timeout = Duration(seconds: 60); Dio _dio = Dio(); String? _cachedToken; // Private constructor NetworkHelper._() { _initializeNetworkHelper(); } // Singleton instance static final NetworkHelper _instance = NetworkHelper._(); // Getter for the singleton instance static NetworkHelper get instance => _instance; // Initial setup of the NetworkHelper Future<void> _initializeNetworkHelper() async { _cachedToken = AppData() .appModel .accessToken; // Fetch and cache the token on initialization _configureDio(); } // Configuration of Dio with initial settings void _configureDio() { final BaseOptions options = BaseOptions( // baseUrl: _baseUrl, responseType: ResponseType.json, connectTimeout: _timeout, receiveTimeout: _timeout, headers: {'Authorization': 'Bearer $_cachedToken','AccessToken': AppData().appModel.cognitoToken.toString(), }, ); _dio = Dio(options); _dio.interceptors.add( InterceptorsWrapper( onRequest: (RequestOptions options, RequestInterceptorHandler handler) async { log("COGNITO TOKEN ${AppData().appModel.cognitoToken}"); // Apply the cached token if (_cachedToken?.isEmpty == true || _cachedToken == null) { _cachedToken = await getToken(); _dio.options.headers = {'Authorization': 'Bearer $_cachedToken','AccessToken': AppData().appModel.cognitoToken.toString(), }; } options.headers['Authorization'] = 'Bearer $_cachedToken'; options.headers['AccessToken'] = AppData().appModel.cognitoToken.toString(); log("HEADERS ${options.headers}"); return handler.next(options); }, onError: (DioException e, ErrorInterceptorHandler handler) async { if (e.response?.statusCode == 401) { // todo: start logout procedure _cachedToken = null; // Invalidate token await StorageHelper.deleteAccessTokenFromStorage(); } return handler.next(e); }, ), ); } // Method to fetch the token Future<String> getToken() async { return AppData().appModel.accessToken; } // Methods for network requests Future<Response> get(String url, {Map<String, dynamic>? queryParameters}) async { try { final Response response = await _dio.get(url, queryParameters: queryParameters); return response; } catch (e) { rethrow; } }}
_cachedToken
this token is JWT token.
AppData().appModel.cognitoToken.toString()
this is AccessToken
log("COGNITO TOKEN ${AppData().appModel.cognitoToken}");
this line print the token too.
i debug the code too, check every value in handlers too, but couldn't find where i am doing wrong. Please help to fix this issue.