forked from zerovm/zwift-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliteauth.js
More file actions
83 lines (75 loc) · 2.29 KB
/
liteauth.js
File metadata and controls
83 lines (75 loc) · 2.29 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
var liteauth = (function () {
var AUTH_ENDPOINT = 'zauth.rax.io';
var AUTH_TYPES = {
GOOGLE: '/login/google',
FACEBOOK: '/login/fb'
};
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
} ();
function login(authType) {
window.location = location.protocol + '//' + AUTH_ENDPOINT
+ authType + '?state=' + encodeURIComponent(location.pathname);
}
function getLoginInfo() {
return QueryString['account'];
}
function getProfile(args) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://' + AUTH_ENDPOINT + '/profile');
xhr.withCredentials = true;
xhr.onload = function (e) {
if (e.target.status == 200) {
args.success(e.target.responseText);
} else {
args.error(e.target.status, e.target.statusText);
}
};
xhr.send();
}
function updateProfile(userKey, inviteCode) {
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://' + AUTH_ENDPOINT + '/profile');
xhr.withCredentials = true;
//xhr.setRequestHeader('X-Auth-User-Key', userKey);
//xhr.setRequestHeader('Content-Type', 'text/plain');
//xhr.setRequestHeader('X-Auth-Invite-Code', inviteCode);
xhr.onload = function (e) {
console.log(e.target.status);
console.log(e.target.statusText);
console.log(e.target.responseText);
console.log(e.target.getResponseHeader('Content-Type'));
};
xhr.onreadystatechange = function () {
console.log(document.readyState);
console.log(arguments);
};
xhr.send('{"user-key": "' + userKey + '"}');
}
return {
AUTH_TYPES: AUTH_TYPES,
login: login,
getProfile: getProfile,
updateProfile: updateProfile,
getLoginInfo: getLoginInfo
};
})();