-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
81 lines (71 loc) · 2.16 KB
/
server.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
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
import express from "express";
import fetch from "node-fetch";
import cookieSession from "cookie-session";
const app = express();
app.use(
cookieSession({
secret: process.env.COOKIE_SECRET
})
);
const client_id = process.env.GITHUB_CLIENT_ID;
const client_secret = process.env.GITHUB_CLIENT_SECRET;
console.log({ client_id, client_secret });
app.get("/", (req, res) => {
res.send("Hello GitHub auth");
});
app.get("/login/github", (req, res) => {
const redirect_uri = "http://localhost:9000/login/github/callback";
res.redirect(
`https://github.com/login/oauth/authorize?client_id=${process.env.GITHUB_CLIENT_ID}&redirect_uri=${redirect_uri}`
);
});
async function getAccessToken({ code, client_id, client_secret }) {
const request = await fetch("https://github.com/login/oauth/access_token", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
client_id,
client_secret,
code
})
});
const text = await request.text();
const params = new URLSearchParams(text);
return params.get("access_token");
}
async function fetchGitHubUser(token) {
const request = await fetch("https://api.github.com/user", {
headers: {
Authorization: "token " + token
}
});
return await request.json();
}
app.get("/login/github/callback", async (req, res) => {
const code = req.query.code;
const access_token = await getAccessToken({ code, client_id, client_secret });
const user = await fetchGitHubUser(access_token);
if (user) {
req.session.access_token = access_token;
req.session.githubId = user.id;
res.redirect("/admin");
} else {
res.send("Login did not succeed!");
}
});
app.get("/admin", async (req, res) => {
if (req.session && req.session.githubId === 1126497) {
res.send("Hello Kevin <pre>" + JSON.stringify(req.session, null, 2));
// Possible use "fetchGitHubUser" with the access_token
} else {
res.redirect("/login/github");
}
});
app.get("/logout", (req, res) => {
if (req.session) req.session = null;
res.redirect("/");
});
const PORT = process.env.PORT || 9000;
app.listen(PORT, () => console.log("Listening on localhost:" + PORT));