You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
821 B

import fastifyJwt from '@fastify/jwt'
import fastifyPlugin from 'fastify-plugin'
/**
* plugin to handle fastify-jwt, descorate function to check for jwt
*
* @param {object} fastify
* @param {object} opts
* @param {Function} done
*
*/
function jwt(fastify, opts, done) {
// register jwt and add JWT_SECRET_KEY
fastify.register(fastifyJwt, {
secret: process.env.JWT_SECRET_KEY,
cookie: {
cookieName: 'token',
signed: false
}
})
// add authenticate
fastify.decorate('authenticate', async function (request, reply) {
try {
await request.jwtVerify()
} catch (error) {
return reply
.code(401)
.send()
}
})
done()
}
export default fastifyPlugin(jwt)