-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.js
More file actions
93 lines (79 loc) · 2.77 KB
/
auth.js
File metadata and controls
93 lines (79 loc) · 2.77 KB
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
/* auth.js
* author: Shane R. Creighton-Young
* project: fbplus
*
*/
/* checkUserData(): Void -> Void
* PRE: true
* POST: calls getUserData
*
* Check that the userData isn't loaded, and if it isn't, then loads it using
* the getUserData function.
*
*/
function checkUserData() {
if (localStorage['userData'] == undefined) {
getUserData();
}
}
/* getUserData(): Void -> Void
* PRE: true
* POST: localStorage['init'] contains a JSON object with the
* user's friends' data
*
* This function uses Facebook's URL authorization system to retrieve an
* access token and query the Facebook Graph API to get data.
*
*/
function getUserData() {
/* This URL requires out application ID, our redirection URL, and other
* options.
*
* NOTE: if you need to query with extra privacy settings, edit the "scope"
* variable. Check the Facebook Graph Explorer to figure out what privary
* requests you need to make.
*/
var scope = "read_stream,offline_access";
var validate_url = "https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=478063252251631&redirect_uri=https://obscure-reaches-7009.herokuapp.com/&scope=" + scope;
var accessToken = "";
var token_url = "";
window.open(validate_url);
chrome.tabs.onUpdated.addListener(
function handler(tabId, changeInfo, tab) {
token_url = changeInfo.url;
if (token_url != undefined && token_url.substring(0,14) == "https://obscur") {
// extracts the access token from the new URL
var begin = token_url.indexOf("#access_token") + 14;
var end = token_url.indexOf("&");
accessToken = token_url.substring(begin, end);
// closes the access token webpage
chrome.tabs.remove(tabId);
/* Facebook Graph API request
* NOTE: this is where one would add fields to increase the amount of
* data to retrieve.
*/
var data_url = "https://graph.facebook.com/me?fields=friends.fields(username,name,gender,hometown,birthday)&access_token=" + accessToken;
// Queries the Facebook Graph API and stores it in localStorage
$.ajax({
url: data_url,
jsonp: 'callback',
dataType: 'jsonp',
success: function (result) {
var userData = result.friends.data;
// sorts the datalist alphabetically by name
function sortFunction(a, b) {
if (a.name < b.name)
return 1;
if (a.name < b.name)
return -1;
return 0;
}
userData.sort(sortFunction);
localStorage['userData'] = JSON.stringify(userData);
}
});
}
}
);
}
checkUserData();