This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 907
/
FBAccessToken.js
227 lines (199 loc) · 6.63 KB
/
FBAccessToken.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
* copy, modify, and distribute this software in source code or binary form for use
* in connection with the web services and APIs provided by Facebook.
*
* As with any software that integrates with the Facebook platform, your use of
* this software is subject to the Facebook Developer Principles and Policies
* [http://developers.facebook.com/policy/]. This copyright notice shall be
* included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @flow
* @format
*/
'use strict';
const AccessToken = require('react-native').NativeModules.FBAccessToken;
const NativeEventEmitter = require('react-native').NativeEventEmitter;
const eventEmitter = new NativeEventEmitter(AccessToken);
type AccessTokenMap = {
accessToken: string,
permissions: Array<string>,
declinedPermissions: Array<string>,
expiredPermissions: Array<string>,
applicationID: string,
userID: string,
expirationTime: number,
lastRefreshTime: number,
dataAccessExpirationTime: number,
accessTokenSource?: string,
};
/**
* Represents an immutable access token for using Facebook services.
*/
class FBAccessToken {
/**
* The access token string.
*/
accessToken: string;
/**
* The known granted permissions.
*/
permissions: Array<string>;
/**
* The known declined permissions.
*/
declinedPermissions: Array<string>;
/**
* The known expired permissions.
*/
expiredPermissions: Array<string>;
/**
* The app ID.
*/
applicationID: string;
/**
* The user ID.
*/
userID: string;
/**
* The expiration time of the access token.
* The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
*/
expirationTime: number;
/**
* The last refresh time of the access token.
* The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
*/
lastRefreshTime: number;
/**
* The data access expiration time of the access token.
* The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
*/
dataAccessExpirationTime: number;
/**
* The source of access token.
* @platform android
*/
accessTokenSource: ?string;
constructor(tokenMap: AccessTokenMap) {
this.accessToken = tokenMap.accessToken;
this.permissions = tokenMap.permissions;
this.declinedPermissions = tokenMap.declinedPermissions;
this.expiredPermissions = tokenMap.expiredPermissions;
this.applicationID = tokenMap.applicationID;
this.userID = tokenMap.userID;
this.expirationTime = tokenMap.expirationTime;
this.lastRefreshTime = tokenMap.lastRefreshTime;
this.dataAccessExpirationTime = tokenMap.dataAccessExpirationTime;
this.accessTokenSource = tokenMap.accessTokenSource;
Object.freeze(this);
}
/**
* Getter for the access token that is current for the application.
*/
static getCurrentAccessToken(): Promise<?FBAccessToken> {
return new Promise((resolve, reject) => {
AccessToken.getCurrentAccessToken((tokenMap) => {
if (tokenMap) {
resolve(new FBAccessToken(tokenMap));
} else {
resolve(null);
}
});
});
}
/**
* Setter for the access token that is current for the application.
*/
static setCurrentAccessToken(accessToken: AccessTokenMap) {
AccessToken.setCurrentAccessToken(accessToken);
}
/**
* Updates the current access token with up to date permissions,
* and extends the expiration date, if extension is possible.
*/
static refreshCurrentAccessTokenAsync(): Promise<any> {
return AccessToken.refreshCurrentAccessTokenAsync();
}
/**
* Adds a listener for when the access token changes. Returns a functions which removes the
* listener when called.
*/
static addListener(
listener: (accessToken: ?FBAccessToken) => void,
): () => void {
const subscription = eventEmitter.addListener(
'fbsdk.accessTokenDidChange',
(tokenMap: AccessTokenMap) => {
if (tokenMap) {
listener(new FBAccessToken(tokenMap));
} else {
listener(null);
}
},
);
return () => subscription.remove();
}
/**
* Gets the date at which the access token expires. The value is the number of
* milliseconds since Jan. 1, 1970, midnight GMT.
*/
getExpires(): number {
return this.expirationTime;
}
/**
* Get the list of permissions associated with this access token. Note that the most up-to-date
* list of permissions is maintained by Facebook, so this list may be outdated if permissions
* have been added or removed since the time the AccessToken object was created. See
* https://developers.facebook.com/docs/reference/login/#permissions for details.
*/
getPermissions(): Array<string> {
return this.permissions;
}
/**
* Gets the list of permissions declined by the user with this access token. It represents the
* entire set of permissions that have been requested and declined. Note that the most
* up-to-date list of permissions is maintained by Facebook, so this list may be outdated if
* permissions have been granted or declined since the last time an AccessToken object was
* created. See https://developers.facebook.com/docs/reference/login/#permissions for details.
*/
getDeclinedPermissions(): Array<string> {
return this.declinedPermissions;
}
getExpiredPermissions(): Array<string> {
return this.expiredPermissions;
}
/**
* Gets the date at which the token was last refreshed. Since tokens expire, the Facebook SDK
* will attempt to renew them periodically. The value is the number of milliseconds since
* Jan. 1, 1970, midnight GMT.
*/
getLastRefresh(): number {
return this.lastRefreshTime;
}
getDataAccessExpiration(): number {
return this.dataAccessExpirationTime;
}
/**
* Gets the ID of the Facebook Application associated with this access token.
*/
getApplicationId(): string {
return this.applicationID;
}
/**
* Gets user ID associated with this access token.
*/
getUserId(): string {
return this.userID;
}
}
module.exports = FBAccessToken;