This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
48 lines (41 loc) · 1.59 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
'use strict'
const buildUdaru = require('@nearform/udaru-core')
const buildConfig = require('./lib/config')
const buildAuthorization = require('./lib/authorization')
const HapiAuthService = require('./lib/authentication')
module.exports = {
pkg: require('./package'),
name: 'udaru-hapi-plugin',
async register (server, options) {
const config = buildConfig(options.config)
const udaru = buildUdaru(options.dbPool, config)
// If there are hooks to register
if (typeof options.hooks === 'object') {
for (const hook of Object.keys(options.hooks)) { // For each hook
// Normalize handlers to always be an array and only consider functions
let handlers = options.hooks[hook]
if (!Array.isArray(handlers)) handlers = [handlers]
handlers = handlers.filter(f => typeof f === 'function')
// Register each handler
for (const handler of handlers) {
udaru.hooks.add(hook, handler)
}
}
}
server.decorate('server', 'udaru', udaru)
server.decorate('server', 'udaruConfig', config)
server.decorate('server', 'udaruAuthorization', buildAuthorization(config))
server.decorate('request', 'udaruCore', udaru)
await server.register([
require('./lib/routes/users'),
require('./lib/routes/policies'),
require('./lib/routes/teams'),
require('./lib/routes/authorization'),
require('./lib/routes/organizations'),
require('./lib/routes/monitor'),
HapiAuthService
])
server.auth.strategy('udaru', 'udaru')
server.auth.default({ mode: 'required', strategy: 'udaru' })
}
}