-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
46 lines (40 loc) · 1.32 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
'use strict'
const fp = require('fastify-plugin')
const verifyBearerAuthFactory = require('./lib/verify-bearer-auth-factory')
const { FST_BEARER_AUTH_INVALID_LOG_LEVEL } = require('./lib/errors')
function fastifyBearerAuth (fastify, options, done) {
options = { addHook: true, verifyErrorLogLevel: 'error', ...options }
if (
Object.hasOwn(fastify.log, 'error') === false ||
typeof fastify.log.error !== 'function'
) {
options.verifyErrorLogLevel = null
}
if (
options.verifyErrorLogLevel != null &&
(
typeof options.verifyErrorLogLevel !== 'string' ||
Object.hasOwn(fastify.log, options.verifyErrorLogLevel) === false ||
typeof fastify.log[options.verifyErrorLogLevel] !== 'function'
)
) {
done(new FST_BEARER_AUTH_INVALID_LOG_LEVEL(options.verifyErrorLogLevel))
}
try {
if (options.addHook === true) {
fastify.addHook('onRequest', verifyBearerAuthFactory(options))
} else {
fastify.decorate('verifyBearerAuthFactory', verifyBearerAuthFactory)
fastify.decorate('verifyBearerAuth', verifyBearerAuthFactory(options))
}
done()
} catch (err) {
done(err)
}
}
module.exports = fp(fastifyBearerAuth, {
fastify: '5.x',
name: '@fastify/bearer-auth'
})
module.exports.default = fastifyBearerAuth
module.exports.fastifyBearerAuth = fastifyBearerAuth