forked from MinhasKamal/DownGit
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauth.html
60 lines (59 loc) · 1.63 KB
/
auth.html
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
<style>
body {
display: flex;
font-family: 'Courier New', Courier, monospace;
justify-content: center;
align-items: center;
}
</style>
<body>
<span id="result">authorizing...</span>
</body>
<script>
function saveAccessInfo(acc) {
const tt = typeof acc === 'string' ? acc : JSON.stringify(acc)
localStorage.setItem('gh-acc', btoa(tt))
}
async function main () {
const query = new URLSearchParams(location.search)
const $result = document.getElementById('result')
if (query.get('error')) {
$result.innerHTML = `<b>Error</b><br>${query.get('error')}<br>${query.get('error_description')}`
return
}
const body = {
appName: 'downGit',
code: query.get('code'),
state: localStorage.getItem('gh-salt')
}
try {
// check gh-auth-worker.js for serverside implement
const result = await fetch('/gh-auth', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}).then(res => res.json())
if (result && result.access_token) {
saveAccessInfo(result)
location.href = location.origin
return
}
$result.innerHTML = `
<b>Error</b> <br>
failed to auth github ${result && result.error_description || ''}<br>
please <a href="/">back to home</a> and auth again
`
console.warn('auth failed', result)
} catch (e) {
$result.innerHTML = `
<b>Error</b> <br>
failed to auth github, it's likely to be a network issue<br>
please reload this page and try a gain
`
console.warn(e)
}
}
main()
</script>