forked from michaelerobertsjr/oauth-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (30 loc) · 965 Bytes
/
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
const express = require('express');
const axios = require('axios');
let app = express();
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/public/index.html'));
});
app.get('/auth', (req, res) => {
res.redirect(
`https://github.com/login/oauth/authorize?client_id=${process.env.CLIENT_ID}`,
);
});
app.get('/oauth-callback', ({ query: { code } }, res) => {
const body = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code,
};
const opts = { headers: { accept: 'application/json' } };
axios
.post('https://github.com/login/oauth/access_token', body, opts)
.then((_res) => _res.data.access_token)
.then((token) => {
// eslint-disable-next-line no-console
console.log('My token:', token);
res.redirect(`/?token=${token}`);
})
.catch((err) => res.status(500).json({ err: err.message }));
});
module.exports = app;