This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (68 loc) · 1.82 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
76
77
78
const Hapi = require('hapi')
const good = require('good');
const hapiAuthJwt = require('hapi-auth-jwt2');
const Knex = require('./db');
// create a server with host and port
const createServer = async () => {
const addRoutes = (server) => {
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return { message: 'Hello world!' }
},
config: {
auth: false,
}
})
server.route(require('./routes/todo'))
server.route(require('./routes/auth'))
server.route(require('./routes/user'))
}
const registerPlugin = async (server) => {
await server.register(hapiAuthJwt)
server.auth.strategy('token', 'jwt', {
key: 'secretkey-hash',
verifyOptions: {
algorithms: ['HS256'],
},
validate: async (decoded, request, h) => {
console.log(" - - - - - - - decoded token:");
console.log(decoded);
console.log(" - - - - - - - request info:");
console.log(request.info);
console.log(" - - - - - - - user agent:");
console.log(request.headers['user-agent']);
const [user] = await Knex('user').where({ id: decoded.id });
return { isValid: user && user.email === decoded.email && user.name === decoded.name }
},
})
// server.auth.default('token')
const goodOptions = {
ops: {
interval: 100000,
},
reporters: {
consoleReporters: [
{ module: 'good-console' },
'stdout'
],
},
}
await server.register({
plugin: good,
options: goodOptions,
})
}
const server = new Hapi.Server({
port: process.env.PORT || 5000,
host: 'localhost',
routes: {
cors: true,
}
})
addRoutes(server)
await registerPlugin(server)
return server
}
module.exports = createServer