-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfb.dart
66 lines (55 loc) · 2.18 KB
/
fb.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'engine/debug.dart';
import 'engine/simple-auth.dart';
import 'engine/visa.dart';
import 'auth-data.dart';
import 'engine/oauth.dart';
/// Enables Facebook [OAuth] authentication
class FacebookAuth extends Visa {
final baseUrl = 'https://www.facebook.com/v8.0/dialog/oauth';
final Debug _debug = Debug(prefix: 'In FacebookAuth ->');
@override
late SimpleAuth visa;
FacebookAuth() {
visa = SimpleAuth(
baseUrl: baseUrl,
/// Sends a request to the user profile api
/// endpoint. Returns an AuthData object.
getAuthData: (Map<String, String> oauthData) async {
if (debugMode) _debug.info('OAuth Data: $oauthData');
final String? token = oauthData[OAuth.TOKEN_KEY];
if (debugMode) _debug.info('OAuth token: $token');
// User profile API endpoint.
final String baseProfileUrlString = 'https://graph.facebook.com/me';
final Uri profileUrl = Uri.parse('$baseProfileUrlString'
'?access_token=$token'
'&fields=first_name,last_name,email');
final http.Response profileResponse = await http.get(profileUrl);
final Map<String, dynamic> profileJson =
json.decode(profileResponse.body);
if (debugMode) _debug.info('Returned Profile Json: $profileJson');
return authData(profileJson, oauthData);
});
}
/// This function combines information
/// from the user [json] and auth response [data]
/// to build an [AuthData] object.
@override
AuthData authData(Map<String, dynamic> json, Map<String, String> data) {
final String? accessToken = data[OAuth.TOKEN_KEY];
final String profileImgUrl = 'https://graph.facebook.com/me/picture'
'?type=large'
'&access_token=$accessToken';
return AuthData(
clientID: data['clientID'],
accessToken: accessToken,
userID: json['id'] as String,
firstName: json['first_name'] as String,
lastName: json['last_name'] as String,
email: json['email'] as String,
profileImgUrl: profileImgUrl,
response: data,
userJson: json);
}
}