-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
125 lines (115 loc) · 3.68 KB
/
index.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>
Login with GitHub – A Cloudflare Worker + GitHub Pages Login Example
</title>
<style>
* {
font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
text-align: center;
}
h1 small {
display: block;
font-size: 1rem;
}
h2 {
margin-top: 10rem;
}
p {
display: none;
}
[data-state="signed-out"] #signed-out,
[data-state="signed-in"] #signed-in,
[data-state="loading"] #loading {
display: block;
}
</style>
</head>
<body data-state="signed-out">
<h1>
Login with GitHub
<small>A Cloudflare Worker + GitHub Pages Login Example</small>
</h1>
<p id="signed-out">
<a href="https://github-oauth-login.gr2m.workers.dev"
>Login with GitHub</a
>
</p>
<p id="signed-in">
Hello there, <span id="login"></span>. (<a href=".">Logout</a>)
</p>
<p id="loading">
Loading...
</p>
<script>
const WORKER_URL = "https://github-oauth-login.gr2m.workers.dev";
const code = new URL(location.href).searchParams.get("code");
const $login = document.querySelector("#login");
if (code) {
login(code);
}
async function login(code) {
// remove ?code=... from URL
const path =
location.pathname +
location.search.replace(/\bcode=\w+/, "").replace(/\?$/, "");
history.pushState({}, "", path);
document.body.dataset.state = "loading";
try {
const response = await fetch(WORKER_URL, {
method: "POST",
mode: "cors",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({ code })
});
const result = await response.json();
if (result.error) {
return alert(JSON.stringify(result, null, 2));
}
// token can now be used to send authenticated requests against https://api.github.com
const getUserResponse = await fetch("https://api.github.com/user", {
headers: {
accept: "application/vnd.github.v3+json",
authorization: `token ${result.token}`
}
});
const { login } = await getUserResponse.json();
$login.textContent = login;
document.body.dataset.state = "signed-in";
} catch (error) {
alert(error);
location.reload();
}
}
</script>
<!-- highlight source code of this page using prism -->
<h2>Source code of this page</h2>
<pre><code class="language-html" id="sourcecode"></code></pre>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.17.1/themes/prism.min.css"
integrity="sha256-77qGXu2p8NpfcBpTjw4jsMeQnz0vyh74f5do0cWjQ/Q="
crossorigin="anonymous"
/>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.17.1/components/prism-core.min.js"
integrity="sha256-Y+Budm2wBEjYjbH0qcJRmLuRBFpXd0VKxl6XhdS4hgA="
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.17.1/plugins/autoloader/prism-autoloader.min.js"
integrity="sha256-ht8ay6ZTPZfuixYB99I5oRpCLsCq7Do2LjEYLwbe+X8="
crossorigin="anonymous"
></script>
<script>
document.querySelector("#sourcecode").textContent =
document.body.parentElement.outerHTML;
</script>
</body>
</html>