completeImplicitGrant method

Uri completeImplicitGrant(
  1. AuthorizationTokenResponse token,
  2. Uri redirectUri, {
  3. String? state,
})

Returns the Uri that a client can be redirected to in the case of an implicit grant.

Implementation

Uri completeImplicitGrant(AuthorizationTokenResponse token, Uri redirectUri,
    {String? state}) {
  var queryParameters = <String, String>{};

  queryParameters.addAll({
    'access_token': token.accessToken,
    'token_type': 'bearer',
  });

  if (state != null) queryParameters['state'] = state;

  if (token.expiresIn != null) {
    queryParameters['expires_in'] = token.expiresIn.toString();
  }

  if (token.scope != null) queryParameters['scope'] = token.scope!.join(' ');

  var fragment =
      queryParameters.keys.fold<StringBuffer>(StringBuffer(), (buf, k) {
    if (buf.isNotEmpty) buf.write('&');
    return buf
      ..write(
        '$k=' + Uri.encodeComponent(queryParameters[k]!),
      );
  }).toString();

  return redirectUri.replace(fragment: fragment);
}