-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (49 loc) · 1.46 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
const Hapi = require('@hapi/hapi');
const Boom = require("@hapi/boom");
const HapiAuthJwt = require("hapi-auth-jwt2");
const Joi = require("joi");
const config = require("./src/config");
const { ConnectDB } = require('./src/config/database');
const RegisterRoutes = require("./src/modules");
const User = require("./src/models/User");
const init = async () => {
try {
await ConnectDB();
console.log("Connected to Database.");
} catch (error) {
console.log(error);
}
const server = Hapi.server({
port: 3000,
host: 'localhost'
});
await server.register(HapiAuthJwt);
server.auth.strategy('jwt', 'jwt',
{
key: config.JWT_SECRET,
validate: async (decoded, request, h) => {
if(await User.findById(decoded._id)) {
request.user = decoded._id;
return { isValid: true }
}
return { isValid: false }
},
verifyOptions: { ignoreExpiration: true }
});
server.auth.default('jwt');
RegisterRoutes(server);
server.route({
method: 'GET',
path: '/api',
options: { auth: false },
handler: (request, h) => {
return h.response({
message: "Hello world!"
}).code(200);
}
});
await server.start();
console.log('Server running on port 3000');
return server;
};
module.exports = init;