"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _AuthService = _interopRequireDefault(require("../services/AuthService"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const authMiddleware = (permissions = []) => {
  const authService = _AuthService.default.getInstance();
  /**
   * 
   * @param {import("fastify").FastifyRequest} request 
   * @param {import("fastify").FastifyReply} reply 
   * @param {Function} done 
   */


  const authHook = (request, reply, done) => {
    const {
      authorization
    } = request.headers || {};
    let jwtToken = authService.extractJWT(authorization);
    let canAccess = authService.verify(jwtToken, permissions);

    if (!canAccess) {
      return reply.status(401).send({
        message: 'FAIL',
        cause: 'unauthorized'
      });
    }

    return done();
  };

  return authHook;
};

var _default = authMiddleware;
exports.default = _default;