/
index.js
55 lines (53 loc) · 1.49 KB
/
index.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
import Meteor from '@meteorrn/core';
import { AccessToken, GraphRequest, GraphRequestManager, LoginManager, Settings } from 'react-native-fbsdk-next';
Meteor.loginWithFacebook = async function(options, callback) {
let scope = ['email'];
if (options?.requestPermissions.length) {
scope = options.requestPermissions;
}
try {
Settings.initializeSDK();
let dataAccessToken = await AccessToken.getCurrentAccessToken();
if (!dataAccessToken) {
const result = await LoginManager.logInWithPermissions(scope);
if (result.isCancelled) {
typeof callback == 'function' && callback({ reason: 'Operation cancelled' });
return;
}
}
const request = new GraphRequest(
'/me',
{
httpMethod: 'GET'
},
async(graphError) => {
if (graphError) {
typeof callback == 'function' && callback(graphError);
return;
}
dataAccessToken = await AccessToken.getCurrentAccessToken();
Meteor._startLoggingIn();
Meteor.call('login', { facebookSignIn: true, ...dataAccessToken }, (error, response) => {
Meteor._endLoggingIn();
Meteor._handleLoginCallback(error, response);
typeof callback == 'function' && callback(error);
});
}
);
new GraphRequestManager().addRequest(request).start();
} catch (error) {
callback(error);
}
};
Meteor.logoutFromFacebook = function() {
return Promise((resolve, reject) => {
Meteor.logout(async(error) => {
if (!error) {
await LoginManager.logOut();
resolve();
} else {
reject(error);
}
});
});
};