authenticate method

Future<CarpUser> authenticate()

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

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> 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);

    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.',
  );
}