angel_auth_twitter 2.0.0

auth_twitter #

angel_auth strategy for Twitter login.

See the example;

2.0.0 #

  • Angel 2 + Dart 2 suppport.
  • Use package:twitter instead of package:twit.
  • Add TwitterAuthorizationException.
  • Add onError callback.

example/main.dart

import 'dart:convert';
import 'dart:io';
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_auth_twitter/angel_auth_twitter.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'package:logging/logging.dart';

class _User {
  final String handle;

  _User(this.handle);

  Map<String, dynamic> toJson() => {'handle': handle};
}

main() async {
  var app = Angel();
  var http = AngelHttp(app);
  var auth = AngelAuth<_User>(
    jwtKey: 'AUTH_TWITTER_SECRET',
    allowCookie: false,
    serializer: (user) async => user.handle,
    deserializer: (screenName) async {
      // Of course, in a real app, you would fetch
      // user data, but not here.
      return _User(screenName.toString());
    },
  );

  auth.strategies['twitter'] = TwitterStrategy(
    ExternalAuthOptions(
      clientId: Platform.environment['TWITTER_CLIENT_ID'] ??
          'qlrBWXneoSYZKS2bT4TGHaNaV',
      clientSecret: Platform.environment['TWITTER_CLIENT_SECRET'] ??
          'n2oA0ZtR7TzYincpMYElRpyYovAQlhYizTkTm2x5QxjH6mLVyE',
      redirectUri: Platform.environment['TWITTER_REDIRECT_URI'] ??
          'http://localhost:3000/auth/twitter/callback',
    ),
    (twit, req, res) async {
      var response = await twit.twitterClient
          .get('https://api.twitter.com/1.1/account/verify_credentials.json');
      var userData = json.decode(response.body) as Map;
      return _User(userData['screen_name'] as String);
    },
    (e, req, res) async {
      // When an error occurs, i.e. the user declines to approve the application.
      if (e.isDenial) {
        res.write("Why'd you say no???");
      } else {
        res.write("oops: ${e.message}");
      }
    },
  );

  app
    ..fallback(auth.decodeJwt)
    ..get('/', auth.authenticate('twitter'));

  app
    ..get(
      '/auth/twitter/callback',
      auth.authenticate(
        'twitter',
        AngelAuthOptions(
          callback: (req, res, jwt) {
            return res.redirect('/home?token=$jwt');
          },
        ),
      ),
    );

  app.get(
    '/home',
    chain([
      requireAuthentication<_User>(),
      (req, res) {
        var user = req.container.make<_User>();
        res.write('Your Twitter handle is ${user.handle}');
        return false;
      },
    ]),
  );

  app.logger = Logger('angel_auth_twitter')
    ..onRecord.listen((rec) {
      print(rec);
      if (rec.error != null) print(rec.error);
      if (rec.stackTrace != null) print(rec.stackTrace);
    });

  await http.startServer('127.0.0.1', 3000);
  print('Listening at ${http.uri}');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  angel_auth_twitter: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:angel_auth_twitter/angel_auth_twitter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
11
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on Aug 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, other

Primary library: package:angel_auth_twitter/angel_auth_twitter.dart with components: io.

Health suggestions

Fix lib/angel_auth_twitter.dart. (-1.49 points)

Analysis of lib/angel_auth_twitter.dart reported 3 hints:

line 95 col 11: DO use curly braces for all flow control structures.

line 97 col 11: DO use curly braces for all flow control structures.

line 104 col 9: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (http).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
angel_auth ^2.0.0 2.1.5+1
angel_framework ^2.0.0-alpha 2.0.4+1
http >=0.11.0 <0.13.0 0.11.3+17 0.12.0+2
path ^1.0.0 1.6.4
twitter ^1.0.0 1.0.0
Transitive dependencies
angel_container 1.0.4
angel_http_exception 1.1.0
angel_model 1.0.3
angel_route 3.0.6
async 2.3.0
charcode 1.1.2
code_buffer 1.0.1
collection 1.14.12
combinator 1.1.0
convert 2.1.1
crypto 2.1.2
dart2_constant 1.0.2+dart2
file 5.0.8+1
http2 1.0.0
http_parser 3.1.3
http_server 0.9.8+3
intl 0.15.8
matcher 0.12.5
merge_map 1.0.2
meta 1.1.7
mime 0.9.6+3
mock_request 1.0.6
oauth 0.4.0
pointycastle 1.0.1
quiver 2.0.5
quiver_hashcode 2.0.0
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
tuple 1.0.2
typed_data 1.1.6
uuid 2.0.2
Dev dependencies
logging ^0.11.0 0.11.3+2
pedantic ^1.0.0 1.8.0+1