authenticateWithUsernamePassword method

Future<CarpUser> authenticateWithUsernamePassword({
  1. required String username,
  2. required String password,
})

Authenticate to this CARP service using a username and password.

The discovery URL in the authProperties is used to find the Identity Server.

Returns the signed in user (with an OAuthToken access token), if successful. Throws a CarpServiceException if not successful.

Implementation

Future<CarpUser> authenticateWithUsernamePassword({
  required String username,
  required String password,
}) async {
  assert(_manager != null, 'Manager not configured. Call configure() first.');
  if (!_manager!.didInit) await initManager();

  final OidcUser? response = await manager?.loginPassword(
    username: username,
    password: password,
  );

  if (response != null) {
    _currentUser = getCurrentUserProfile(response);

    if (_currentUser != null) {
      _currentUser!
          .authenticated(OAuthToken.fromTokenResponse(response.token));
      _authEventController.add(AuthEvent.authenticated);
      return currentUser;
    }
  }

  // All other cases are treated as a failed attempt
  _authEventController.add(AuthEvent.failed);

  throw CarpServiceException(
    httpStatus: HTTPStatus(401),
    message: 'Authentication failed.',
  );
}