-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
75 lines (71 loc) · 1.79 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const hapi = require('hapi');
const inert = require('inert');
const path = require('path');
const fs = require('fs');
const querystring = require('querystring');
const request = require('request');
require('env2')('./config.env');
const server = new hapi.Server();
server.connection({
port: 4000,
tls: {
key: fs.readFileSync('./keys/key.pem'),
cert: fs.readFileSync('./keys/cert.pem')
}
});
server.register(inert, (err) => {
server.route([
{
method: 'GET',
path: '/{file*}',
handler: {
directory: {
path: path.join(__dirname, 'public'),
index: 'hello.html'
}
}
},
{
method: 'GET',
path: '/login',
handler: (request, reply) => {
const queries = querystring.stringify({
client_id: process.env.CLIENT_ID,
redirect_uri: 'https://localhost:4000/welcome'
});
reply.redirect(`https://github.com/login/oauth/authorize?${queries}`);
}
},
{
method: 'GET',
path: '/welcome',
handler: (req, reply) => {
const data = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code: req.query.code
};
const options = {
method: 'POST',
body: data,
json: true,
url: 'https://github.com/login/oauth/access_token'
};
request(options, (error, response, body) => {
if (error) {
return reply(error);
}
if (!body.access_token) {
reply('something went wrong.')
}
// store access token
reply.redirect('/');
})
}
}
]);
});
server.start((err) => {
if (err) throw err;
console.log(`Server listening on port ${server.info.port}`);
});