authenticate function

Future<Credential?> authenticate(
  1. Client client, {
  2. List<String> scopes = const [],
  3. Map<String, String>? queryParameters,
})

Implementation

Future<Credential?> authenticate(
  Client client, {
  List<String> scopes = const [],
  Map<String, String>? queryParameters,
}) async {
  var q = queryParameters;

  AuthorizationCodeParameters? parameters;

  if (q != null &&
      q.containsKey('state') &&
      q.containsKey('code') &&
      q.containsKey('session_state')) {
    parameters = AuthorizationCodeParameters(
      code: q['code']!,
      state: q['state']!,
      sessionState: q['session_state']!,
      iss: q['iss']!,
    );
  }

  if (parameters != null) {
    window.history.replaceState('', '', makeReturnUrl());
    window.localStorage.remove('openid_client:state');
    window.localStorage['openid_client:state'] = parameters.state;
    return authenticateWithAuthorizationCode(
      client,
      parameters.code,
      parameters.state,
      parameters.sessionState,
      parameters.iss,
    );
  }

  var flow = makeFlow(client, scopes: scopes);
  window.localStorage['openid_client:state'] = flow.state;

  var result =
      await launchUrl(flow.authenticationUri, webOnlyWindowName: '_self');

  if (!result) {
    throw Exception('Action annulée.');
  }
  return null;
}