fetchTokenByRefreshToken function

Future<LogtoRefreshTokenResponse> fetchTokenByRefreshToken({
  1. required Client httpClient,
  2. required String tokenEndPoint,
  3. required String clientId,
  4. required String refreshToken,
  5. String? resource,
  6. List<String>? scopes,
})

Implementation

Future<LogtoRefreshTokenResponse> fetchTokenByRefreshToken({
  required http.Client httpClient,
  required String tokenEndPoint,
  required String clientId,
  required String refreshToken,
  String? resource,
  List<String>? scopes,
}) async {
  Map<String, dynamic> payload = {
    'grant_type': refreshTokenGrantType,
    'client_id': clientId,
    'refresh_token': refreshToken,
  };

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

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

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

  var body = httpResponseHandler(response);

  return LogtoRefreshTokenResponse.fromJson(body);
}