-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
52 lines (41 loc) · 1.46 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
import express from 'express';
import fetch from 'node-fetch';
// Import environment variables
const clientID = process.env.GITHUB_CLIENT_ID;
const clientSecret = process.env.GITHUB_CLIENT_SECRET;
const frontendAddress = process.env.FRONT_END_ADDRESS;
// Create application
const app = express();
// Create parser
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Set up the port
const PORT = process.env.port || 9000;
app.listen(PORT, () => console.log('Listening http://localhost:' + PORT));
// Passes the code on to GitHub, then returns the access token
app.post('/get_access_token', (req, res) => {
const code = req.body.code;
getAccessToken(code)
.then(accessToken => {
res.setHeader('Access-Control-Allow-Origin', frontendAddress);
res.end(JSON.stringify({ access_token: accessToken }));
});
});
// Exchanges the code with GitHub to receive the Access Token
async function getAccessToken(code) {
const request = await fetch(`https://github.com/login/oauth/access_token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
clientID,
clientSecret,
code
})
});
const data = await request.text();
const params = new URLSearchParams(data);
return params.get('access_token');
}