"use strict";

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

var _crypto = require("crypto");

var _accessToken = require("../repository/accessToken");

class AccessTokenMiddleware {
  static async authorize(req, res, next) {
    const token = req.header('Access-Token') || typeof req.query.token === 'string' && req.query.token;

    if (!token) {
      next();
      return;
    }

    const hashedToken = (0, _crypto.createHash)('sha256').update(token, 'utf-8').digest('hex');
    const accessToken = await _accessToken.accessTokenRepository.findOne({
      token: hashedToken
    });

    if (!accessToken) {
      next();
      return;
    }

    req.user = accessToken.userId;
    next();
  }

}

exports.AccessTokenMiddleware = AccessTokenMiddleware;
//# sourceMappingURL=token.js.map