getValidAccessTokenOrThrow method

Future<String> getValidAccessTokenOrThrow()

Returns current access token if sufficiently valid, otherwise refreshes. Throws if not logged in or refresh fails.

Implementation

Future<String> getValidAccessTokenOrThrow() async {
  final token = MeshagentAuth.current.getAccessToken();

  if (token == null || token.isEmpty) {
    throw StateError('Not logged in');
  }

  if (_isAccessTokenValidEnough()) {
    return token;
  }

  await refreshOrThrow();

  final newToken = MeshagentAuth.current.getAccessToken();
  if (newToken == null || newToken.isEmpty) {
    throw StateError('Refresh succeeded but access token missing');
  }

  return newToken;
}