UNPKG

@ngn-net/giftcard-shared

Version:

Shared library for gifts score microservices

36 lines 1.92 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var RevokerThrottlerGuard_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.RevokerThrottlerGuard = void 0; const common_1 = require("@nestjs/common"); const user_service_1 = require("./user/user.service"); const throttler_1 = require("@nestjs/throttler"); let RevokerThrottlerGuard = RevokerThrottlerGuard_1 = class RevokerThrottlerGuard extends throttler_1.ThrottlerGuard { constructor() { super(...arguments); this.logger = new common_1.Logger(RevokerThrottlerGuard_1.name); this.userService = new user_service_1.UserService(); } async throwThrottlingException(context) { var _a; const request = context.switchToHttp().getRequest(); const userId = (_a = request.auth) === null || _a === void 0 ? void 0 : _a.sub; if (userId) { const jti = request.auth.jti; await this.userService.revokeToken(jti); this.logger.warn(`Rate limit exceeded for user: ${userId}`); } throw new throttler_1.ThrottlerException("Too many requests"); } }; exports.RevokerThrottlerGuard = RevokerThrottlerGuard; exports.RevokerThrottlerGuard = RevokerThrottlerGuard = RevokerThrottlerGuard_1 = __decorate([ (0, common_1.Injectable)() ], RevokerThrottlerGuard); //# sourceMappingURL=throtler.guard.js.map