UNPKG

@brewww/authentication-service

Version:
104 lines 4.12 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.OtpService = void 0; const typeorm_1 = require("typeorm"); const common_1 = require("@nestjs/common"); const entities_1 = require("../entities"); const typeorm_2 = require("@nestjs/typeorm"); const config_1 = require("@nestjs/config"); const error_1 = require("../error"); const otp_value_1 = require("../utils/otp-value"); const crypto_1 = require("crypto"); let OtpService = class OtpService { constructor(otpRepository, configService) { this.otpRepository = otpRepository; this.configService = configService; } async validateOtpAsync(channel, otpValue) { const otpEntity = await this.otpRepository.findOne({ where: { value: otpValue, channel: (0, typeorm_1.JsonContains)(channel), expiresAt: (0, typeorm_1.MoreThan)(new Date()), }, }); return !!otpEntity; } async validateEmailOtpAsync(email, otpValue) { return await this.validateOtpAsync({ email }, otpValue); } async validatePhoneOtpAsync(phone, otpValue) { return await this.validateOtpAsync({ phone, }, otpValue); } async createEmailOtpAsync(email) { return await this.createOtpAsync({ email, }); } async createPhoneOtpAsync(phone) { return await this.createOtpAsync({ phone: phone }); } async expireOtpAsync(channel) { const entity = await this.otpRepository.findOne({ where: { channel: (0, typeorm_1.JsonContains)(channel), expiresAt: (0, typeorm_1.MoreThan)(new Date()), }, }); if (!entity) throw new error_1.OtpNotFoundError(); this.otpRepository.update(entity.id, { expiresAt: new Date(), }); } async createOtpAsync(channel) { const activeOtp = await this.otpRepository.findOne({ where: { channel: (0, typeorm_1.JsonContains)(channel), expiresAt: (0, typeorm_1.MoreThan)(new Date()), }, }); if (activeOtp) return { isSent: false, expiresAt: activeOtp.expiresAt }; const otpEntity = await this.otpRepository.save({ value: otp_value_1.OtpValue.generate(6), channel, expiresAt: new Date(new Date().getTime() + this.configService.get("otp.expiresIn") * 1000), }); return { isSent: true, expiresAt: otpEntity.expiresAt, otpValue: otpEntity.value, }; } createFakeOtpResult() { return { isSent: (0, crypto_1.randomInt)(0, 2) === 1, expiresAt: new Date(new Date().getTime() + this.configService.get("otp.expiresIn") * 1000), }; } }; OtpService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, typeorm_2.InjectRepository)(entities_1.Otp)), __metadata("design:paramtypes", [typeorm_1.Repository, config_1.ConfigService]) ], OtpService); exports.OtpService = OtpService; //# sourceMappingURL=otp.service.js.map