-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (31 loc) · 1.08 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
import express from 'express';
import request from 'request';
const port = 9000;
const AUTHORIZE_URL = `https://github.com/login/oauth/authorize?client_id=${process.env.GITHUB_CLIENTID}&redirect_uri=http://localhost:9000&scope=user:email,read:user&state=QaWsEdRfTg&allow_signup=false`;
const TOKEN_URL = `https://github.com/login/oauth/access_token?client_id=${process.env.GITHUB_CLIENTID}&client_secret=${process.env.GITHUB_CLIENT_SECRET}&state=QaWsEdRfTg&code=`;
const app = express();
app.use(express.static(__dirname));
app.get('/github-login', (req, res) => {
res.redirect(AUTHORIZE_URL);
});
app.get('/github-code', (req, res) => {
const params = req.query;
const url = TOKEN_URL + params.code;
request.post(url,{
headers: {
'Accept': 'application/json'
}
},(err, resp, body) => {
if (err) {
console.log('Error occurred when getting token', err);
}
res.status(200).send(body);
})
});
app.listen(port, function(err) {
if(err) {
console.log(err);
return;
}
console.log(`Server running at http://localhost:${port}`);
});