authenticate method

Future<CarpUser> authenticate()

Authenticate to this CARP service, that opens the authentication page of the Identity Server using a secure web view from the OS.

The discovery URL in the app 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> authenticate() async {
  assert(_manager != null, 'Manager not configured. Call configure() first.');

  if (!_manager!.didInit) {
    await initManager();
  }

  OidcUser? response = await manager!.loginAuthorizationCodeFlow();

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

  // All other cases are treated as a failed attempt and throws an error
  _authEventController.add(AuthEvent.failed);
  _currentUser = null;

  // auth error response from CARP is in the form
  throw CarpServiceException(
    httpStatus: HTTPStatus(401),
    message: 'Authentication failed.',
  );
}