Skip to content

Commit cbbe791

Browse files
committed
Add test
1 parent e103a7a commit cbbe791

File tree

2 files changed

+493
-7
lines changed

2 files changed

+493
-7
lines changed

lib/src/client/streamable_https.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,16 @@ class StreamableHttpClientTransport implements Transport {
497497
return;
498498
}
499499

500+
// Check for authentication first - if we need auth, handle it before proceeding
501+
if (_authProvider != null) {
502+
final tokens = await _authProvider!.tokens();
503+
if (tokens == null) {
504+
// No tokens available - trigger authentication flow
505+
await _authProvider!.redirectToAuthorization();
506+
throw UnauthorizedError('Authentication required');
507+
}
508+
}
509+
500510
final headers = await _commonHeaders();
501511
headers['content-type'] = 'application/json';
502512
headers['accept'] = 'application/json, text/event-stream';
@@ -523,13 +533,9 @@ class StreamableHttpClientTransport implements Transport {
523533

524534
if (response.statusCode < 200 || response.statusCode >= 300) {
525535
if (response.statusCode == 401 && _authProvider != null) {
526-
final result = await auth(_authProvider!, serverUrl: _url);
527-
if (result != "AUTHORIZED") {
528-
throw UnauthorizedError();
529-
}
530-
531-
// Purposely _not_ awaited, so we don't call onerror twice
532-
return send(message);
536+
// Authentication failed with the server - try to refresh or redirect
537+
await _authProvider!.redirectToAuthorization();
538+
throw UnauthorizedError('Authentication failed with the server');
533539
}
534540

535541
final text = await response.transform(utf8.decoder).join();

0 commit comments

Comments
 (0)