getAuthHeader method

Future<MapEntry<String, String>?> getAuthHeader()

Returns authentication headers for the current user, or null if there is no current user.

Implementation

Future<MapEntry<String, String>?> getAuthHeader() async {
  if (_logInFuture != null) await _logInFuture;
  if (_logOutFuture != null) await _logOutFuture;

  void updateCurrentUser(AuthUser<T>? user) {
    if (user?.id == currentUserValue?.id) {
      persistence.setCurrentUser(user);
    }
  }

  currentUser.first.then(
    (value) async {
      if (value != null && value.token.isExpired && _tokenFuture == null) {
        _tokenFuture = _runOrHandleTokenError(
          () => client.ensureUpdatedToken(value).then(updateCurrentUser),
        )
            .catchError(_tokenErrorController.addError)
            .whenComplete(() => _tokenFuture = null);
      }
    },
  );

  return currentUser
      .mergeWith([_tokenErrorController.stream])
      .firstWhere((user) => user == null || !user.token.isExpired)
      .then(
        (user) => user != null
            ? MapEntry(user.token.authHeaderKey, user.token.authHeaderValue)
            : null,
      );
}