platform/example/server.dart

53 lines
1.6 KiB
Dart
Raw Normal View History

2016-12-23 02:07:47 +00:00
import 'dart:io';
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_auth_twitter/angel_auth_twitter.dart';
import 'package:angel_diagnostics/angel_diagnostics.dart';
import 'package:angel_framework/angel_framework.dart';
2017-02-25 20:21:07 +00:00
import 'package:twit/twit.dart';
2016-12-23 02:07:47 +00:00
const Map<String, String> TWITTER_CONFIG = const {
'callback': 'http://localhost:3000/auth/twitter/callback',
'key': 'qlrBWXneoSYZKS2bT4TGHaNaV',
'secret': 'n2oA0ZtR7TzYincpMYElRpyYovAQlhYizTkTm2x5QxjH6mLVyE'
};
2017-02-25 20:21:07 +00:00
verifier(Twit twit) async {
// Maybe fetch user credentials:
return await twit.get('/account/verify_credentials.json');
}
2016-12-23 02:07:47 +00:00
main() async {
var app = new Angel();
var auth = new AngelAuth(jwtKey: 'AUTH_TWITTER_SECRET', allowCookie: false);
await app.configure(auth);
2017-02-25 20:21:07 +00:00
auth.serializer = (user) async => user['screen_name'];
2016-12-23 02:07:47 +00:00
2017-02-25 20:21:07 +00:00
auth.deserializer = (screenName) async {
2016-12-23 02:07:47 +00:00
// Of course, in a real app, you would fetch
// user data, but not here.
2017-02-25 20:21:07 +00:00
return {'handle': '@$screenName'};
2016-12-23 02:07:47 +00:00
};
2017-02-25 20:21:07 +00:00
auth.strategies.add(new TwitterStrategy(verifier, config: TWITTER_CONFIG));
2016-12-23 02:07:47 +00:00
app
..get('/', auth.authenticate('twitter'))
..get(
'/auth/twitter/callback',
auth.authenticate('twitter',
new AngelAuthOptions(callback: (req, res, jwt) {
return res.redirect('/home?token=$jwt');
})))
..chain('auth').get('/home', (req, res) {
res
2017-02-25 20:21:07 +00:00
..write('Your Twitter handle is ${req.user["handle"]}.')
2016-12-23 02:07:47 +00:00
..end();
});
2017-02-25 20:21:07 +00:00
await app.configure(logRequests(new File('log.txt')));
var server = await app.startServer(null, 3000);
print('Listening at http://${server.address.address}:${server.port}');
2016-12-23 02:07:47 +00:00
}