refresh method

Future<CarpUser> refresh()

Get a new access token for the current user based on the previously granted refresh token, using the Identity Server discovery URL.

This method is typically used when the access token has expired, and a new access token is needed to access the CARP web service. The refresh token expiration date is OAuthToken.expiresAt.

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

Implementation

Future<CarpUser> refresh() async {
  assert(_manager != null, 'Manager not configured. Call configure() first.');
  if (!_manager!.didInit) await initManager();

  final OidcUser? response = await manager?.refreshToken();

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