initiateAuth method

Future<CognitoUserSession?> initiateAuth(
  1. AuthenticationDetails authDetails
)

This is used for authenticating the user through the custom authentication flow.

Implementation

Future<CognitoUserSession?> initiateAuth(
    AuthenticationDetails authDetails) async {
  final authParameters = (authDetails.getAuthParameters() ?? []).fold({},
      (dynamic value, element) {
    value[element.name] = element.value;
    return value;
  });
  authParameters['USERNAME'] = username;

  if (_clientSecretHash != null) {
    authParameters['SECRET_HASH'] = _clientSecretHash;
  }

  final paramsReq = {
    'AuthFlow': 'CUSTOM_AUTH',
    'ClientId': pool.getClientId(),
    'AuthParameters': authParameters,
    'ClientMetadata': authDetails.getValidationData(),
  };

  if (getUserContextData() != null) {
    paramsReq['UserContextData'] = getUserContextData();
  }

  final data = await client!.request('InitiateAuth',
      await _analyticsMetadataParamsDecorator.call(paramsReq));

  final String? challengeName = data['ChallengeName'];
  final challengeParameters = data['ChallengeParameters'];
  if (challengeName == 'CUSTOM_CHALLENGE') {
    _session = data['Session'];
    throw CognitoUserCustomChallengeException(
        challengeParameters: challengeParameters);
  }

  _signInUserSession = getCognitoUserSession(data['AuthenticationResult']);
  await cacheTokens();

  return _signInUserSession;
}