Skip to content

Commit

Permalink
Provide pre-constructed token interceptors from Passputter instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Bryant committed Feb 22, 2022
1 parent be77d85 commit ad797e2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.0

- Add pre-constructed `ClientTokenInterceptor` and `UserTokenInterceptor` to `Passputter`

## 2.0.1

- Fix build
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Easily authenticate using OAuth 2.0 client/password grants.
Install passputter from [pub.dev](https://pub.dev/packages/passputter):

```yaml
passputter: ^2.0.1
passputter: ^2.1.0
```
## ✅ Prerequisites
Expand Down Expand Up @@ -128,6 +128,14 @@ Passputter provides two interceptors which you can add to your Dio instances: `U

`ClientTokenInterceptor` will add a client token to all requests. If no token is saved in your `TokenStorage`, the interceptor will attempt to generate one before continuing.

You can retrieve them from your `Passputter` instance:

```dart
dio.interceptors.add(passputter.clientTokenInterceptor);
// or
dio.interceptors.add(passputter.userTokenInterceptor);
```

### 💰 Step 4: Profit

That's it! You now have a fully working authentication system for your Flutter app.
Expand Down
16 changes: 16 additions & 0 deletions lib/src/passputter_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ class PassputterImpl implements Passputter {
Future<void> logOut() async {
await tokenStorage.deleteUserToken();
}

@override
ClientTokenInterceptor get clientTokenInterceptor => ClientTokenInterceptor(
tokenStorage: tokenStorage,
oAuthApi: oAuthApi,
clientId: clientId,
clientSecret: clientSecret,
);

@override
UserTokenInterceptor get userTokenInterceptor => UserTokenInterceptor(
tokenStorage: tokenStorage,
oAuthApi: oAuthApi,
clientId: clientId,
clientSecret: clientSecret,
);
}
7 changes: 6 additions & 1 deletion lib/src/passputter_interface.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// 📦 Package imports:
import 'package:dio/dio.dart';

// 🌎 Project imports:
import 'package:passputter/passputter.dart';
import 'package:passputter/src/oauth_api_impl.dart';
Expand Down Expand Up @@ -51,4 +50,10 @@ abstract class Passputter {

/// Log out of the application
Future<void> logOut();

/// A [Dio] interceptor which adds a client token to each request
ClientTokenInterceptor get clientTokenInterceptor;

/// A [Dio] interceptor which adds a user token to each request
UserTokenInterceptor get userTokenInterceptor;
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: passputter
description: Easily authenticate using OAuth 2.0 client/password grants.
version: 2.0.1
version: 2.1.0
repository: https://github.com/netsells/passputter

environment:
Expand Down

0 comments on commit ad797e2

Please sign in to comment.