-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
84 lines (73 loc) · 2.45 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { OpenAPIBackend } from 'openapi-backend'
import Fastify, { FastifyInstance, FastifyReply } from 'fastify'
import { routes, handlers } from './routes/index'
import fastifyMongodb from '@fastify/mongodb'
import fastifyJwt from '@fastify/jwt'
import { document } from './schema/schema'
import dotenv from 'dotenv'
import { FastifyReplyType } from 'fastify/types/type-provider'
dotenv.config({ path: 'env.local' })
// TODO: Divide up this file to make it easier to read
export const api = new OpenAPIBackend({
definition: document, // need to break out this document per resource to make more readable
strict: true,
validate: true,
handlers,
quick: true,
apiRoot: '/movies'
})
api.init()
export const fastify = function () {
const app: FastifyInstance = Fastify({
logger: {
transport: {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname'
}
}
}
})
app.register(fastifyJwt, {
secret: 'supersecret',
messages: customMessages
})
// Connect to database
app.register(fastifyMongodb, {
// Close the connection when the app stops
forceClose: true,
url: `mongodb+srv://${process.env.MONGODB_ATLAS_CLUSTER_USERNAME}:${process.env.MONGODB_ATLAS_CLUSTER_PASSWORD}@aws-movie-library-clust.demazqw.mongodb.net/movie-library`
})
app.register(routes)
app.addHook('onRequest', async (request: any, reply) => {
if (request.url !== '/auth/login') {
await api.securityHandlers.jwt(request, reply)
}
})
return app
}
const customMessages = {
badRequestErrorMessage: 'Format is Authorization: Bearer [token]',
noAuthorizationInHeaderMessage: 'Autorization header is missing!',
authorizationTokenExpiredMessage: 'Authorization token expired',
authorizationTokenInvalid: (err: { message: string }): string => {
return `Authorization token is invalid: ${err.message}`
}
}
// Since we want authentication on all endpoints, we can register this handler on every request.
// If we wanted authentication on only a few requests, we could use a decorator instead
// https://github.com/fastify/fastify-jwt#usage
api.registerSecurityHandler('jwt', async (request: any, reply: any): Promise<void> => {
try {
await request.jwtVerify()
} catch (err) {
reply.send(err)
}
})
api.register('unauthorizedHandler', (reply: any) => {
const response: ErrorResponse = {
message: 'unauthorized'
}
return reply.code(401).send(response)
})