UNPKG

@bitwild/rockets-auth

Version:

Rockets Auth - Complete authentication and authorization solution for NestJS with JWT, OAuth, OTP, role-based access control, and more

94 lines 4.61 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); } }; var RocketsAuthOtpService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.RocketsAuthOtpService = void 0; const nestjs_common_1 = require("@concepta/nestjs-common"); const nestjs_otp_1 = require("@concepta/nestjs-otp"); const common_1 = require("@nestjs/common"); const rockets_auth_constants_1 = require("../../../shared/constants/rockets-auth.constants"); const rockets_auth_notification_service_1 = require("./rockets-auth-notification.service"); const rockets_auth_exception_1 = require("../../../shared/exceptions/rockets-auth.exception"); const error_logging_helper_1 = require("../../../shared/utils/error-logging.helper"); let RocketsAuthOtpService = RocketsAuthOtpService_1 = class RocketsAuthOtpService { constructor(settings, userModelService, otpService, otpNotificationService) { this.settings = settings; this.userModelService = userModelService; this.otpService = otpService; this.otpNotificationService = otpNotificationService; this.logger = new common_1.Logger(RocketsAuthOtpService_1.name); } async sendOtp(email) { try { const user = await this.userModelService.byEmail(email); const { assignment, category, expiresIn } = this.settings.otp; if (user) { const otp = await this.otpService.create({ assignment, otp: { category, type: 'uuid', assigneeId: user.id, expiresIn: expiresIn, }, }); await this.otpNotificationService.sendOtpEmail({ email, passcode: otp.passcode, }); this.logger.log('OTP sent successfully', { category, expiresIn, timestamp: new Date().toISOString(), }); } else { this.logger.log('OTP request for non-existent user'); } } catch (error) { const { errorMessage } = (0, error_logging_helper_1.logAndGetErrorDetails)(error, this.logger, 'OTP send failed', { errorId: 'OTP_SEND_FAILED' }); if (error instanceof nestjs_common_1.RuntimeException) { throw error; } else { throw new rockets_auth_exception_1.RocketsAuthException(errorMessage); } } } async confirmOtp(email, passcode) { const { assignment, category } = this.settings.otp; const user = await this.userModelService.byEmail(email); if (!user) { throw new nestjs_otp_1.OtpException(); } const isValid = await this.otpService.validate(assignment, { category: category, passcode, }, true); if (!isValid) { throw new nestjs_otp_1.OtpException(); } return user; } }; exports.RocketsAuthOtpService = RocketsAuthOtpService; exports.RocketsAuthOtpService = RocketsAuthOtpService = RocketsAuthOtpService_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(rockets_auth_constants_1.ROCKETS_AUTH_MODULE_OPTIONS_DEFAULT_SETTINGS_TOKEN)), __param(1, (0, common_1.Inject)(rockets_auth_constants_1.RocketsAuthUserModelService)), __param(3, (0, common_1.Inject)(rockets_auth_notification_service_1.RocketsAuthNotificationService)), __metadata("design:paramtypes", [Object, Object, nestjs_otp_1.OtpService, Object]) ], RocketsAuthOtpService); //# sourceMappingURL=rockets-auth-otp.service.js.map