fetchTokenByAuthorizationCode function

Future<LogtoCodeTokenResponse> fetchTokenByAuthorizationCode({
  1. required Client httpClient,
  2. required String tokenEndPoint,
  3. required String code,
  4. required String codeVerifier,
  5. required String clientId,
  6. required String redirectUri,
  7. String? resource,
})

Implementation

Future<LogtoCodeTokenResponse> fetchTokenByAuthorizationCode(
    {required http.Client httpClient,
    required String tokenEndPoint,
    required String code,
    required String codeVerifier,
    required String clientId,
    required String redirectUri,
    String? resource}) async {
  Map<String, dynamic> payload = {
    'grant_type': authorizationCodeGrantType,
    'code': code,
    'code_verifier': codeVerifier,
    'client_id': clientId,
    'redirect_uri': redirectUri,
  };

  if (resource != null && resource.isNotEmpty) {
    payload.addAll({'resource': resource});
  }

  final response = await httpClient.post(Uri.parse(tokenEndPoint),
      headers: {'Content-Type': _requestContentType}, body: payload);

  var body = httpResponseHandler(response);

  return LogtoCodeTokenResponse.fromJson(body);
}