-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (44 loc) · 1.31 KB
/
index.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
const express = require("express")
const bodyParser = require("body-parser")
const fetch = require("node-fetch")
const cors = require('cors')
const { client_id, client_secret } = require("./config")
const app = express()
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.json({ type: "text/*" }))
app.use(bodyParser.urlencoded({ extended: false }))
app.post("/authenticate", (req, res) => {
const { code } = req.body
// Request to exchange code for an access token
fetch(`https://github.com/login/oauth/access_token?client_id=${client_id}&client_secret=${client_secret}&code=${code}`, {
method: "POST",
headers: {
Accept: "application/json"
}
})
.then((response) => response.json())
.then((response) => {
res.status(200).json(response)
})
.catch((error) => {
return res.status(400).json(error)
});
});
app.get("/profile", (req, res) => {
const { access_token } = req.headers
fetch(`https://api.github.com/user`, {
headers: {
Authorization: `token ${access_token}`,
},
})
.then((response) => response.json())
.then((response) => {
return res.status(200).json(response)
})
.catch((error) => {
return res.status(400).json(error)
});
})
const PORT = process.env.PORT || 5000
app.listen(PORT, () => console.log(`Listening on ${PORT}`))