-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (41 loc) · 1.2 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
require('dotenv').config();
var axios = require('axios');
var express = require('express');
var app = express();
var exphbs = require('express-handlebars');
var config = require('./config')
//cors headers
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
//template engine added
app.set('views', './views');
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
//route to get acess_token
app.get('/oauth', async function (req, res) {
const code = req.query.code
try {
const { data } = await axios({
method: config.oauth_method,
url: config.oauth_host + config.oauth_path,
data: {
client_id: config.oauth_client_id,
client_secret: config.oauth_client_secret,
code
},
});
if (data.includes('access_token')) {
res.send(data)
}
} catch (error) {
console.error('Error ' + error.message)
}
});
//server init
app.listen(config.port, function () {
console.log(`github oauth redirect server is listening on port ${config.port}!`);
});