forked from danba340/oauth-github-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (34 loc) · 1.09 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
require('dotenv').config();
const axios = require('axios');
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static('static'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/static/index.html'));
});
app.get('/auth', (req, res) => {
res.redirect(
`https://github.com/login/oauth/authorize?client_id=${process.env.GITHUB_CLIENT_ID}`,
);
});
app.get('/oauth-callback', ({ query: { code } }, res) => {
const body = {
client_id: process.env.GITHUB_CLIENT_ID,
client_secret: process.env.GITHUB_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 }));
});
app.listen(3000);
// eslint-disable-next-line no-console
console.log('App listening on port 3000');