2025-03-10 00:24:26 +01:00

26 lines
690 B
JavaScript

const fp = require('fastify-plugin');
const jwt = require('@fastify/jwt');
const bcrypt = require('bcrypt');
module.exports = fp(async function (fastify, opts) {
fastify.register(jwt, {
secret: process.env.JWT_SECRET
});
fastify.decorate('authenticate', async function (request, reply) {
try {
await request.jwtVerify();
} catch (err) {
reply.code(401).send({ error: 'Non authentifié' });
}
});
fastify.decorate('hashPassword', async (password) => {
const saltRounds = 10;
return bcrypt.hash(password, saltRounds);
});
fastify.decorate('comparePassword', async (password, hash) => {
return bcrypt.compare(password, hash);
});
});