-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (58 loc) · 1.75 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
const Path = require('path');
const Hapi = require('hapi');
const Good = require('good');
const Inert = require('inert');
const HapiAuthJWT = require('hapi-auth-jwt2');
const User = require('./models/user');
const hash = require('./utils').hash;
const CONFIG = require('./config');
const server = new Hapi.Server();
server.connection({address: 'localhost', port: 8080});
var validate = function (decoded, request, callback) {
User.findById(decoded.id, (err, user) => {
if ((err || !user) ||
(decoded.ua !== hash(user.ua)) ||
(decoded.ua !== hash(request.headers['user-agent']))) {
return callback(null, false);
}
return callback(null, true, user);
});
};
server.register([
{register: HapiAuthJWT, options: {}},
{register: require('h2o2'), options: {}},
{register: Inert, options: {}},
{register: Good,
options: {
ops: {
interval: CONFIG.DAY_IN_MILLISECONDS
},
reporters: {
console: [{
module: 'good-console',
args: [{log: '*', response: '*'}]
}, 'stdout']
}
}
}
],
(err) => {
if (err) {
console.error(err);
return;
}
server.auth.strategy('jwt', 'jwt',
{ key: CONFIG.SECRET,
validateFunc: validate,
verifyOptions: {algorithms: ['HS256']}
});
server.route(require('./routes'));
server.ext('onPreResponse', function (request, reply) {
if (request.response.isBoom) {
return reply.redirect('/');
}
return reply.continue();
});
server.start(() => console.log(`server started at ${server.info.uri} NODE_ENV=${process.env.NODE_ENV}`));
});