requestAuthorizationCode function

Future<CodeResponse> requestAuthorizationCode({
  1. required String clientId,
  2. required Iterable<String> scopes,
  3. String? state,
  4. String? hint,
  5. String? hostedDomain,
  6. @Deprecated('Undocumented feature. Do not include in production code.') String? logLevel,
})

Implementation

Future<CodeResponse> requestAuthorizationCode({
  required String clientId,
  required Iterable<String> scopes,
  String? state,
  String? hint,
  String? hostedDomain,
  @Deprecated('Undocumented feature. Do not include in production code.')
  String? logLevel,
}) async {
  await gis_loader.loadWebSdk();
  if (logLevel != null) _googleAccountsId.callMethod('setLogLevel', [logLevel]);

  final completer = Completer<CodeResponse>();

  void callback(gis.CodeResponse response) {
    if (response.error != null) {
      window.console.log(response);
      completer.completeError(
        AuthenticationException(
          response.error!,
          errorDescription: response.error_description,
          errorUri: response.error_uri,
        ),
      );
      return;
    }

    completer.complete(CodeResponse._(
      code: response.code!,
      scopes: response.scope,
      state: response.state,
    ));
  }

  void errorCallback(gis.GoogleIdentityServicesError? error) {
    if (error != null) {
      completer.completeError(
        AuthenticationException(
          error.type.toString(),
          errorDescription: error.message,
          errorUri:
              'https://developers.google.com/identity/oauth2/web/reference/js-reference#TokenClientConfig',
        ),
      );
    }
  }

  final config = gis.CodeClientConfig(
    callback: allowInterop(callback),
    client_id: clientId,
    scope: scopes.toList(),
    state: state,
    login_hint: hint,
    hd: hostedDomain,
    error_callback: allowInterop(errorCallback),
  );

  final client = gis.oauth2.initCodeClient(config);

  client.requestCode();

  return completer.future;
}