UNPKG

mongodb-dynamic-api

Version:

Auto generated CRUD API for MongoDB using NestJS

193 lines 10 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 __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthGatewayMixin = void 0; const common_1 = require("@nestjs/common"); const swagger_1 = require("@nestjs/swagger"); const websockets_1 = require("@nestjs/websockets"); const lodash_1 = require("lodash"); const gateways_1 = require("../../../gateways"); const helpers_1 = require("../../../helpers"); const mixins_1 = require("../../../mixins"); const change_password_dto_1 = require("../dtos/change-password.dto"); const reset_password_dto_1 = require("../dtos/reset-password.dto"); const guards_1 = require("../guards"); const auth_policies_guard_mixin_1 = require("./auth-policies-guard.mixin"); function AuthGatewayMixin(userEntity, { loginField, passwordField, abilityPredicate: loginAbilityPredicate, useInterceptors: loginUseInterceptors = [], }, { additionalFields: additionalSocketRegisterFields, protected: registerProtected, abilityPredicate: registerAbilityPredicate, useInterceptors: registerUseInterceptors = [], } = {}, { resetPasswordUseInterceptors = [], changePasswordUseInterceptors = [], ...resetPasswordOptions } = {}, { useInterceptors: updateAccountUseInterceptors = [], ...updateAccountOptions } = {}) { var _a; class AuthSocketBodyPasswordFieldDto extends (0, swagger_1.PickType)(userEntity, [passwordField]) { } _a = passwordField; __decorate([ (0, swagger_1.ApiProperty)(), __metadata("design:type", String) ], AuthSocketBodyPasswordFieldDto.prototype, _a, void 0); class AuthSocketLoginDto extends (0, swagger_1.IntersectionType)((0, swagger_1.PickType)(userEntity, [loginField]), AuthSocketBodyPasswordFieldDto) { } const additionalSocketMandatoryFields = []; const additionalSocketOptionalFields = []; if (!additionalSocketRegisterFields) { additionalSocketRegisterFields = []; } additionalSocketRegisterFields.forEach((field) => { if (typeof field === 'string') { additionalSocketOptionalFields.push(field); return; } const { required, name } = field; if (required) { additionalSocketMandatoryFields.push(name); } else { additionalSocketOptionalFields.push(name); } }); class AuthSocketRegisterDto extends (0, swagger_1.IntersectionType)((0, swagger_1.PickType)(userEntity, [loginField, ...additionalSocketMandatoryFields]), additionalSocketOptionalFields?.length ? (0, swagger_1.IntersectionType)(AuthSocketBodyPasswordFieldDto, (0, swagger_1.PartialType)((0, swagger_1.PickType)(userEntity, additionalSocketOptionalFields))) : AuthSocketBodyPasswordFieldDto) { } class AuthUpdateAccountDto extends (0, mixins_1.EntityBodyMixin)(userEntity, true, [ loginField, passwordField, ...updateAccountOptions.additionalFieldsToExclude ?? [], ]) { } class AuthRegisterPoliciesGuard extends (0, auth_policies_guard_mixin_1.AuthSocketPoliciesGuardMixin)(userEntity, registerAbilityPredicate) { } class AuthUpdateAccountPoliciesGuard extends (0, auth_policies_guard_mixin_1.AuthSocketPoliciesGuardMixin)(userEntity, updateAccountOptions.abilityPredicate) { } const getAccountEvent = 'auth-get-account'; const updateAccountEvent = 'auth-update-account'; const loginEvent = 'auth-login'; const registerEvent = 'auth-register'; const resetPasswordEvent = 'auth-reset-password'; const changePasswordEvent = 'auth-change-password'; class BaseAuthGateway extends gateways_1.BaseGateway { constructor(service, jwtService) { super(jwtService); this.service = service; this.jwtService = jwtService; } async getAccount(socket) { return { event: getAccountEvent, data: socket.user ? await this.service.getAccount(socket.user) : undefined, }; } async updateAccount(socket, body) { return { event: updateAccountEvent, data: socket.user ? await this.service.updateAccount(socket.user, body) : undefined, }; } async login(socket, { [loginField]: login, [passwordField]: password }) { if (login && password) { socket.user = await this.service.validateUser(login, password); } if (!socket.user) { throw new websockets_1.WsException('Unauthorized'); } if (loginAbilityPredicate && !loginAbilityPredicate(socket.user)) { throw new websockets_1.WsException('Access denied'); } return { event: loginEvent, data: await this.service.login(socket.user), }; } async register(socket, data) { this.addUserToSocket(socket, !registerProtected && !registerAbilityPredicate); return { event: registerEvent, data: await this.service.register(data), }; } async resetPassword({ email }) { if ((0, lodash_1.isEmpty)(resetPasswordOptions)) { throw new websockets_1.WsException('This feature is not enabled'); } return { event: resetPasswordEvent, data: await this.service.resetPassword(email), }; } async changePassword({ resetPasswordToken, newPassword }) { if ((0, lodash_1.isEmpty)(resetPasswordOptions)) { throw new websockets_1.WsException('This feature is not enabled'); } return { event: changePasswordEvent, data: await this.service.changePassword(resetPasswordToken, newPassword), }; } } __decorate([ (0, common_1.UseGuards)(new guards_1.JwtSocketAuthGuard()), (0, websockets_1.SubscribeMessage)(getAccountEvent), __param(0, (0, websockets_1.ConnectedSocket)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], BaseAuthGateway.prototype, "getAccount", null); __decorate([ (0, common_1.UseGuards)(new guards_1.JwtSocketAuthGuard(), new AuthUpdateAccountPoliciesGuard()), (0, common_1.UseInterceptors)(...updateAccountUseInterceptors), (0, websockets_1.SubscribeMessage)(updateAccountEvent), __param(0, (0, websockets_1.ConnectedSocket)()), __param(1, (0, websockets_1.MessageBody)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, AuthUpdateAccountDto]), __metadata("design:returntype", Promise) ], BaseAuthGateway.prototype, "updateAccount", null); __decorate([ (0, common_1.UseInterceptors)(...loginUseInterceptors), (0, websockets_1.SubscribeMessage)(loginEvent), __param(0, (0, websockets_1.ConnectedSocket)()), __param(1, (0, websockets_1.MessageBody)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, AuthSocketLoginDto]), __metadata("design:returntype", Promise) ], BaseAuthGateway.prototype, "login", null); __decorate([ (0, common_1.UseGuards)(new AuthRegisterPoliciesGuard()), (0, common_1.UseInterceptors)(...registerUseInterceptors), (0, websockets_1.SubscribeMessage)(registerEvent), __param(0, (0, websockets_1.ConnectedSocket)()), __param(1, (0, websockets_1.MessageBody)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, AuthSocketRegisterDto]), __metadata("design:returntype", Promise) ], BaseAuthGateway.prototype, "register", null); __decorate([ (0, common_1.UseGuards)(new guards_1.ResetPasswordGuard((0, helpers_1.isNotEmptyObject)(resetPasswordOptions))), (0, common_1.UseInterceptors)(...resetPasswordUseInterceptors), (0, websockets_1.SubscribeMessage)(resetPasswordEvent), __param(0, (0, websockets_1.MessageBody)()), __metadata("design:type", Function), __metadata("design:paramtypes", [reset_password_dto_1.ResetPasswordDto]), __metadata("design:returntype", Promise) ], BaseAuthGateway.prototype, "resetPassword", null); __decorate([ (0, common_1.UseGuards)(new guards_1.ResetPasswordGuard((0, helpers_1.isNotEmptyObject)(resetPasswordOptions))), (0, common_1.UseInterceptors)(...changePasswordUseInterceptors), (0, websockets_1.SubscribeMessage)(changePasswordEvent), __param(0, (0, websockets_1.MessageBody)()), __metadata("design:type", Function), __metadata("design:paramtypes", [change_password_dto_1.ChangePasswordDto]), __metadata("design:returntype", Promise) ], BaseAuthGateway.prototype, "changePassword", null); return BaseAuthGateway; } exports.AuthGatewayMixin = AuthGatewayMixin; //# sourceMappingURL=auth-gateway.mixin.js.map