-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.js
50 lines (40 loc) · 1.07 KB
/
oauth.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
(function init(window){
let token = '';
if(!window) {
return;
}
window.addEventListener('load', onLoad.bind(window,window.location.search));
function onLoad(query) {
if(query.search("code") != -1) {
let [,code] = query.match(/\?code=(.*)/);
let [,state] = query.match(/state=(.*)/);
if(code) {
getAccessTokenFromApi(code,state);
}
}
}
function getAccessTokenFromApi(code,state) {
let $result = axios.get("http://localhost:9000/github-code",{
params: {
code: code.substr(0, code.indexOf('&')),
}
})
$result.then(resp => {
console.log(`Access token -> `, resp);
token = resp.data.access_token;
getUserInfo();
}).catch(err => {
console.log(`Error occurred when getting access_token -> ${err}`)
});
}
function getUserInfo() {
let $result = axios.get('https://api.github.com/user',{
headers: {
Authorization: `token ${token}`
}
});
$result.then(resp => {
console.log('user info -> ', resp.data);
})
}
}(window))