forked from HynesIP/meteor-accounts-facebook-cordova
-
Notifications
You must be signed in to change notification settings - Fork 12
/
facebook_server.js
58 lines (44 loc) · 1.66 KB
/
facebook_server.js
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
Accounts.registerLoginHandler(function(loginRequest) {
if(!loginRequest.cordova) {
return undefined;
}
loginRequest = loginRequest.authResponse;
var whitelisted = ['id', 'email', 'name', 'first_name',
'last_name', 'link', 'gender', 'locale', 'age_range'];
var identity = getIdentity(loginRequest.accessToken, whitelisted);
var profilePicture = getProfilePicture(loginRequest.accessToken);
console.log(profilePicture);
var serviceData = {
accessToken: loginRequest.accessToken,
expiresAt: (+new Date) + (1000 * loginRequest.expiresIn)
};
var fields = _.pick(identity, whitelisted);
_.extend(serviceData, fields);
var options = {profile: {}};
var profileFields = _.pick(identity, Meteor.settings.public.facebook.profileFields);
_.extend(options.profile, profileFields);
options.profile.avatar = profilePicture;
return Accounts.updateOrCreateUserFromExternalService("facebook", serviceData, options);
});
var getIdentity = function (accessToken, fields) {
try {
return HTTP.get("https://graph.facebook.com/me", {
params: {
access_token: accessToken,
fields: fields.join(",")
}
}).data;
} catch (err) {
throw _.extend(new Error("Failed to fetch identity from Facebook. " + err.message),
{response: err.response});
}
};
var getProfilePicture = function (accessToken) {
try {
return HTTP.get("https://graph.facebook.com/v2.8/me/picture/?redirect=false", {
params: {access_token: accessToken}}).data.data.url;
} catch (err) {
throw _.extend(new Error("Failed to fetch identity from Facebook. " + err.message),
{response: err.response});
}
};