-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
66 lines (55 loc) · 1.54 KB
/
index.ts
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
import * as hapi from "@hapi/hapi";
import Vision from "@hapi/vision";
import Inert from "@hapi/inert";
import HapiSwagger from "hapi-swagger";
import HapiAuthJwt2 from "hapi-auth-jwt2";
import fs from "fs";
import config from "./config";
import connectDB from "./lib/dbConnect";
import setRoutes from "./routes";
const validateUser = async (decoded, request, h) => {
// Perform validation of the decoded JWT token
// Return an error if validation fails, or return a user object if validation succeeds
// throw Boom.unauthorized("Invalid credentials");.
return { isValid: true, userId: decoded.userId };
// return "OK";
};
const init = async () => {
await connectDB();
const server: hapi.Server = new hapi.Server({
port: 5000,
routes: { cors: { origin: ["*"] } },
host: "0.0.0.0",
});
await server.register(Inert);
await server.register(Vision);
await server.register({
plugin: HapiSwagger,
options: {
info: {
title: "My API",
version: "1.0.0",
},
securityDefinitions: {
jwt: {
type: "apiKey",
name: "Authorization",
in: "header",
},
},
},
});
await server.register(HapiAuthJwt2);
await server.auth.strategy("jwt", "jwt", {
key: config.jwtSecret,
validate: validateUser,
verifyOptions: { algorithms: ["HS256"] },
});
// server.auth.default("jwt");
await setRoutes(server);
await server.start();
console.log(`🚀 Server running on ${server.info.uri} 🚀`);
return server;
};
init();
export default init;