-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
41 lines (31 loc) · 1.16 KB
/
script.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
function showProfile(data){
const profile_data = JSON.stringify(data);
document.querySelector('body').innerHTML = profile_data;
}
function getProfile(data) {
const access_token = data;
localStorage.setItem('access_token',access_token);
fetch(`https://api.github.com/user?access_token=${access_token}`)
.then(data => data.json())
.then(data => showProfile(data))
.catch(err => console.error(err));
}
function getAccessToken() {
if(access_token = localStorage.getItem('access_token'))
getProfile(access_token);
const clientId = `14a973416657e234da0a`;
let code = window.location.search;
code = code.replace("?code=", '');
console.log('Got the code', code);
fetch(`http://localhost:5000/${clientId}/${code}`)
.then(data => data.json())
.then(data => getProfile(data.access_token))
.catch(err => console.error(err));
}
function doWeHaveAccessToken(){
if(access_token = localStorage.getItem('access_token')){
getProfile(access_token);
}
}
document.querySelector('button').addEventListener('click', getAccessToken)
document.addEventListener('DOMContentLoaded', doWeHaveAccessToken);