2019-03-02 00:03:48 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
2024-10-13 01:45:27 +00:00
|
|
|
import 'package:protevus_auth/protevus_auth.dart';
|
|
|
|
import 'package:protevus_auth_twitter/protevus_auth_twitter.dart';
|
|
|
|
import 'package:protevus_framework/protevus_framework.dart';
|
|
|
|
import 'package:protevus_framework/http.dart';
|
2019-03-02 00:03:48 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
class _User {
|
|
|
|
final String handle;
|
|
|
|
|
|
|
|
_User(this.handle);
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() => {'handle': handle};
|
|
|
|
}
|
|
|
|
|
2021-06-20 13:29:23 +00:00
|
|
|
void main() async {
|
2024-10-12 10:35:14 +00:00
|
|
|
var app = Protevus();
|
|
|
|
var http = ProtevusHttp(app);
|
2024-10-13 02:17:24 +00:00
|
|
|
var auth = ProtevusAuth<_User>(
|
2019-03-02 00:03:48 +00:00
|
|
|
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 {
|
2022-06-04 01:30:18 +00:00
|
|
|
var response = await twit.client.get(Uri.parse(
|
2021-05-02 07:32:24 +00:00
|
|
|
'https://api.twitter.com/1.1/account/verify_credentials.json'));
|
2019-03-02 00:03:48 +00:00
|
|
|
var userData = json.decode(response.body) as Map;
|
|
|
|
return _User(userData['screen_name'] as String);
|
|
|
|
},
|
2019-04-11 14:28:51 +00:00
|
|
|
(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 {
|
2021-06-20 13:29:23 +00:00
|
|
|
res.write('oops: ${e.message}');
|
2019-04-11 14:28:51 +00:00
|
|
|
}
|
|
|
|
},
|
2019-03-02 00:03:48 +00:00
|
|
|
);
|
|
|
|
|
2022-06-04 01:30:18 +00:00
|
|
|
app.get('/', auth.authenticate('twitter'));
|
2019-03-02 00:03:48 +00:00
|
|
|
|
2021-06-20 13:29:23 +00:00
|
|
|
app.get(
|
|
|
|
'/auth/twitter/callback',
|
|
|
|
auth.authenticate(
|
|
|
|
'twitter',
|
2024-10-13 02:17:24 +00:00
|
|
|
ProtevusAuthOptions(
|
2021-06-20 13:29:23 +00:00
|
|
|
callback: (req, res, jwt) {
|
|
|
|
return res.redirect('/home?token=$jwt');
|
|
|
|
},
|
2019-03-02 00:03:48 +00:00
|
|
|
),
|
2021-06-20 13:29:23 +00:00
|
|
|
),
|
|
|
|
);
|
2019-03-02 00:03:48 +00:00
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/home',
|
|
|
|
chain([
|
|
|
|
requireAuthentication<_User>(),
|
|
|
|
(req, res) {
|
2022-06-04 01:30:18 +00:00
|
|
|
var user = req.container!.make<_User>();
|
2019-03-02 00:03:48 +00:00
|
|
|
res.write('Your Twitter handle is ${user.handle}');
|
2019-04-11 14:28:51 +00:00
|
|
|
return false;
|
2019-03-02 00:03:48 +00:00
|
|
|
},
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
|
|
|
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}');
|
|
|
|
}
|