@@ -4,24 +4,65 @@ import 'package:chopper/chopper.dart';
4
4
import 'package:oauth_chopper/oauth_chopper.dart' ;
5
5
import 'package:oauth_chopper/src/extensions/request.dart' ;
6
6
7
+ /// Callback for error handling.
8
+ typedef OnErrorCallback = void Function (Object , StackTrace );
9
+
7
10
/// {@template oauth_interceptor}
8
11
/// OAuthInterceptor is responsible for adding 'Authorization' header to
9
12
/// requests.
10
13
/// The header is only added if there is a token available. When no token is
11
14
/// available no header is added.
12
15
/// Its added as a Bearer token.
16
+ ///
17
+ /// When the provided credentials are invalid it tries to refresh them.
18
+ /// Can throw a exceptions if no [onError] is passed. When [onError] is passed
19
+ /// exception will be passed to [onError]
13
20
/// {@endtemplate}
14
- class OAuthInterceptor implements RequestInterceptor {
21
+ class OAuthInterceptor implements Interceptor {
15
22
/// {@macro oauth_interceptor}
16
- OAuthInterceptor (this .oauthChopper);
23
+ OAuthInterceptor (this .oauthChopper, this .onError);
24
+
25
+ /// Callback for error handling.
26
+ final OnErrorCallback ? onError;
17
27
18
28
/// The [OAuthChopper] instance to get the token from.
19
29
final OAuthChopper oauthChopper;
20
30
21
31
@override
22
- FutureOr <Request > onRequest (Request request) async {
32
+ FutureOr <Response <BodyType >> intercept <BodyType >(
33
+ Chain <BodyType > chain,
34
+ ) async {
35
+ // Add oauth token to the request.
23
36
final token = await oauthChopper.token;
24
- if (token == null ) return request;
25
- return request.addAuthorizationHeader (token.accessToken);
37
+
38
+ final Request request;
39
+ if (token == null ) {
40
+ request = chain.request;
41
+ } else {
42
+ request = chain.request.addAuthorizationHeader (token.accessToken);
43
+ }
44
+
45
+ final response = await chain.proceed (request);
46
+
47
+ // If the response is unauthorized and a token is available try to
48
+ // refresh 1 time.
49
+ if (response.statusCode == 401 && token != null ) {
50
+ try {
51
+ final credentials = await oauthChopper.refresh ();
52
+ if (credentials != null ) {
53
+ final request =
54
+ chain.request.addAuthorizationHeader (credentials.accessToken);
55
+ return chain.proceed (request);
56
+ }
57
+ } catch (e, s) {
58
+ if (onError != null ) {
59
+ onError? .call (e, s);
60
+ } else {
61
+ rethrow ;
62
+ }
63
+ }
64
+ }
65
+
66
+ return response;
26
67
}
27
68
}
0 commit comments