-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfacebookConnectPlugin-angular.js
175 lines (142 loc) · 5.04 KB
/
facebookConnectPlugin-angular.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Angular module wrapping facebookConnectPlugin.js for PhoneGap/Cordova.
* Let's you test Facebook interaction either on a device/emulator (native), or in a browser (JS SDK).
*
* Based on OpenFB library by Christophe Coenraets @ccoenraets: https://github.com/ccoenraets/OpenFB
*
* For a facebookConnectPlugin.js example, see https://github.com/phonegap/phonegap-facebook-plugin/tree/develop
*
* @author Duncan Smith @dunc_smiff
* @version 0.1
*/
angular.module('facebookConnectPlugin', [])
.factory('FBC', function ($rootScope, $q, $window, $http) {
var fbAppId,
tokenStore = window.sessionStorage, // override in init
runningInCordova;
document.addEventListener("deviceready", function () {
runningInCordova = true;
}, false);
/**
* Initialize the module. You must use this function and initialize the module with an appId before you can
* use any other function.
* @param appId - The id of the Facebook app
* @param store - The store used to save the Facebook token. Optional. If not provided, we use sessionStorage.
*/
function init(appId, store) {
console.log('FBC init');
fbAppId = appId;
if (store) tokenStore = store;
if (!window.cordova) {
console.log('No Cordova - using browser init');
window.fbAsyncInit = function() {
facebookConnectPlugin.browserInit(fbAppId);
};
}
}
/**
* Login to Facebook. If running in a Browser, the OAuth workflow happens in a a popup window.
* @param fbScope - The set of Facebook permissions requested
*/
function login(fbScope) {
console.log('FBC login');
if (!fbAppId) {
return error({error: 'Facebook App Id not set.'});
}
fbScope = fbScope || '';
deferredLogin = $q.defer();
logout();
facebookConnectPlugin.login( ["email"],
function success(response) {
if (response.status == 'connected') {
console.log('FBC connected');
// Not strictly necessary to store token, but can be used by app to check if logged in
tokenStore['fbtoken'] = response.token;
deferredLogin.resolve();
}
else {
alert('not connected');
deferredLogin.reject();
}
},
function error(response) {
console.log('login error');
deferredLogin.reject(response);
});
return deferredLogin.promise;
}
/**
* Application-level logout: we simply discard the token.
*/
function logout() {
tokenStore['fbtoken'] = undefined;
}
/**
* Helper function to de-authorize the app
* @param success
* @param error
* @returns {*}
*/
function revokePermissions() {
// return api({method: 'DELETE', path: '/me/permissions'})
// .success(function () {
// console.log('Permissions revoked');
// });
}
/**
* Lets you make any Facebook Graph API request.
* @path: path in the Facebook graph: /me, /me.friends, etc. Required.
* @permissions: for native API call. Optional array of strings.
*/
function api(path, permissions) {
console.log('api: ' + path);
var deferred = $q.defer();
permissions = permissions || [];
facebookConnectPlugin.api(path, permissions, // path, permissions,
function success(response) {
console.log('api success: ' + JSON.stringify(response));
deferred.resolve(response);
},
function error(response) {
//e.g. response = "(#200) Requires extended permission: read_stream"
console.log('api failed: ' + JSON.stringify(response));
deferred.reject(response);
});
//todo: handle auth exception
// return $http({method: method, url: 'https://graph.facebook.com' + obj.path, params: params})
// .error(function(data, status, headers, config) {
// if (data.error && data.error.type === 'OAuthException') {
// $rootScope.$emit('OAuthException');
// }
// });
return deferred.promise;
}
//todo: these helpers are pretty pointless at the moment
/**
* Helper function for a POST call into the Graph API
* @param path
* @param permissions
* @returns {*}
*/
function post(path, permissions) {
return api(path, permissions);
}
/**
* Helper function for a GET call into the Graph API
* @param path
* @param permissions
* @returns {*}
*/
function get(path, permissions) {
return api(path, permissions);
}
return {
init: init,
login: login,
logout: logout,
revokePermissions: revokePermissions,
api: api,
post: post,
get: get
}
});