@brewww/authentication-service
Version:
Authenticator service for Brew projects.
124 lines • 6.94 kB
JavaScript
;
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.AuthNotificationService = void 0;
const common_1 = require("@nestjs/common");
const template_service_1 = require("../template/template.service");
const event_emitter_1 = require("@nestjs/event-emitter");
const config_1 = require("@nestjs/config");
const enum_1 = require("../enum");
const error_1 = require("./error");
const dto_1 = require("./dto");
const otp_sms_template_not_found_error_ts_1 = require("./error/otp-sms-template-not-found.error.ts");
const nestjs_notification_module_1 = require("@brewww/nestjs-notification-module/");
let AuthNotificationService = class AuthNotificationService {
constructor(templateService, notificationService, configService) {
this.templateService = templateService;
this.notificationService = notificationService;
this.configService = configService;
this.defaultLocale = this.configService.get("notificationDefaultLocale");
}
async onOtpEmailCreatedAsync(otpEmailCreatedEvent) {
var _a;
const email = this.getOtpEmailByAuthAction(otpEmailCreatedEvent.otpValue, otpEmailCreatedEvent.authenticationAction, (_a = otpEmailCreatedEvent === null || otpEmailCreatedEvent === void 0 ? void 0 : otpEmailCreatedEvent.locale) !== null && _a !== void 0 ? _a : this.defaultLocale, otpEmailCreatedEvent === null || otpEmailCreatedEvent === void 0 ? void 0 : otpEmailCreatedEvent.appData);
if (email) {
return await this.sendEmailAsync(email.subject, otpEmailCreatedEvent.emailAddress, email.content);
}
}
async onOtpSmsCreatedAsync(otpSmsCreatedEvent) {
var _a;
const sms = this.getOtpSmsByAuthAction(otpSmsCreatedEvent.otpValue, otpSmsCreatedEvent.authenticationAction, (_a = otpSmsCreatedEvent === null || otpSmsCreatedEvent === void 0 ? void 0 : otpSmsCreatedEvent.locale) !== null && _a !== void 0 ? _a : this.defaultLocale, otpSmsCreatedEvent === null || otpSmsCreatedEvent === void 0 ? void 0 : otpSmsCreatedEvent.appData);
if (sms) {
this.notificationService.sendSms({
message: sms.message,
phoneNumber: otpSmsCreatedEvent.phoneNumber,
});
}
}
async onResetPasswordEmailCreatedAsync(resetPasswordCreatedEvent) {
var _a;
const resetPasswordEmailTemplate = this.templateService.getResetPasswordEmailTemplate((_a = resetPasswordCreatedEvent === null || resetPasswordCreatedEvent === void 0 ? void 0 : resetPasswordCreatedEvent.locale) !== null && _a !== void 0 ? _a : this.defaultLocale);
const emailContent = this.templateService.injectData(resetPasswordEmailTemplate, {
resetLink: resetPasswordCreatedEvent.resetLink,
});
return await this.sendEmailAsync(this.configService.get("emailSubjects.resetPassword"), resetPasswordCreatedEvent.emailAddress, emailContent);
}
getOtpEmailByAuthAction(otpValue, authenticationAction, locale, appData) {
let template = null;
if (authenticationAction === enum_1.AuthenticationAction.LOGIN)
template = this.templateService.getLoginOtpEmailTemplate(locale);
if (authenticationAction === enum_1.AuthenticationAction.SIGNUP)
template = this.templateService.getSignupOtpEmailTemplate(locale);
if (template == null)
throw new error_1.OtpEmailTemplateNotFoundError(authenticationAction);
return {
content: this.templateService.injectData(template, {
otpValue,
appData,
}),
subject: this.configService.get("emailSubjects.loginOtp"),
};
}
getOtpSmsByAuthAction(otpValue, authenticationAction, locale, appData) {
let template = null;
if (authenticationAction === enum_1.AuthenticationAction.LOGIN)
template = this.templateService.getLoginOtpSmsTemplate(locale);
if (authenticationAction === enum_1.AuthenticationAction.SIGNUP)
template = this.templateService.getSignUpOtpSmsTemplate(locale);
if (template == null)
throw new otp_sms_template_not_found_error_ts_1.OtpSmsTemplateNotFoundError(authenticationAction);
return {
message: this.templateService.injectData(template, {
otpValue,
appData,
}),
};
}
async sendEmailAsync(subject, to, content) {
return await this.notificationService.sendEmail({
from: this.configService.get("emailFrom"),
to,
subject,
content,
});
}
};
__decorate([
(0, event_emitter_1.OnEvent)("otp.email.created"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [dto_1.OtpEmailCreatedEvent]),
__metadata("design:returntype", Promise)
], AuthNotificationService.prototype, "onOtpEmailCreatedAsync", null);
__decorate([
(0, event_emitter_1.OnEvent)("otp.sms.created"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [dto_1.OtpSmsCreatedEvent]),
__metadata("design:returntype", Promise)
], AuthNotificationService.prototype, "onOtpSmsCreatedAsync", null);
__decorate([
(0, event_emitter_1.OnEvent)("reset-password.email.created"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [dto_1.ResetPasswordCreatedEvent]),
__metadata("design:returntype", Promise)
], AuthNotificationService.prototype, "onResetPasswordEmailCreatedAsync", null);
AuthNotificationService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)("TemplateService")),
__param(1, (0, common_1.Inject)("NotificationService")),
__metadata("design:paramtypes", [template_service_1.TemplateService,
nestjs_notification_module_1.NotificationService,
config_1.ConfigService])
], AuthNotificationService);
exports.AuthNotificationService = AuthNotificationService;
//# sourceMappingURL=auth-notification.service.js.map