oauth_dio 0.0.2 copy "oauth_dio: ^0.0.2" to clipboard
oauth_dio: ^0.0.2 copied to clipboard

outdated

A customizable oauth client with token storage and interceptors for dio

oauth_dio #

A customizable oauth client with token storage and interceptors for dio.

Getting Started #

Instantiate a new OAuth Client:

// myclient.dart
import 'package:oauth_dio/oauth_dio.dart';

final oauth = OAuth(
      tokenUrl: '<YOUR TOKEN URL>',
      clientId: '<YOUR CLIENT ID>',
      clientSecret: '<YOUR SECRET>');

Obtaining an access token using username and password:

OAuthToken token = oauth.requestToken(
    grantType: 'password',
    username: '<YOUR USERNAME>',
    password: '<YOUR PASSWORD>'
).then((token) {
    print(token.accessToken);
});

Updating access token using a refresh token:

OAuthToken token = oauth.requestToken(
    grantType: 'refresh_token',
    refreshToken: '<YOUR REFRESH TOKEN>'
).then((token) {
    print(token.accessToken);
});

Configuring Dio to send access tokens: #

Instantiate a new OAuth Client with a permanent storage, by default oauth is configured with memory storage.

In this example we will use the flutter_secure_storage plugin to store the token on the device's keychain.

// myclient.dart
import 'package:oauth_dio/oauth_dio.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';


class OAuthSecureStorage extends OAuthStorage {
  final FlutterSecureStorage storage;
  final accessTokenKey = 'accessToken';
  final refreshTokenKey = 'refreshToken';

  @override
  Future<OAuthToken> fetch() async {
    return OAuthToken(
        accessToken: await storage.read(key: accessTokenKey),
        accessToken: await storage.read(key: refreshTokenKey);
    )
  }

  @override
  Future<OAuthToken> save(OAuthToken token) async {
    await storage.write(key: accessTokenKey, value: token.acessToken);
    await storage.write(key: refreshTokenKey, value: token.refreshToken);
    return token;
  }

  Future<void> clear() async {
    await storage.delete(key: accessTokenKey);
    await storage.delete(key: refreshTokenKey);
  }
}

final oauth = OAuth(
    tokenUrl: '<YOUR TOKEN URL>',
    clientId: '<YOUR CLIENT ID>',
    clientSecret: '<YOUR SECRET>',
    storage: OAuthSecureStorage()
);

final authenticadedDio = Dio()
authenticadedDio.interceptors.add(BearerInterceptor(oauth: oauth))


authenticadedDio.get('/my/protected/resource').then((response) {
    print(response.data);
})

Feedback #

Please feel free to give me any feedback helping support this package!

55
likes
0
pub points
82%
popularity

Publisher

unverified uploader

A customizable oauth client with token storage and interceptors for dio

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

dio, flutter

More

Packages that depend on oauth_dio