UNPKG

@denz93/vendure-plugin-simple-auth

Version:

Allow customers login using email and verification code (One time login)

109 lines 4.87 kB
"use strict"; /* https://docs.nestjs.com/modules */ 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var SimpleAuthPlugin_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleAuthPlugin = void 0; const common_1 = require("@nestjs/common"); const core_1 = require("@vendure/core"); const email_plugin_1 = require("@vendure/email-plugin"); const constants_1 = require("@vendure/email-plugin/lib/src/constants"); const path_1 = __importDefault(require("path")); const constants_2 = require("./constants"); const copy_dir_1 = require("./copy-dir"); const email_handler_1 = require("./email-handler"); const schema_1 = require("./schema"); const simple_auth_strategy_1 = require("./simple-auth-strategy"); const simple_auth_resolver_1 = require("./simple-auth.resolver"); const simple_auth_service_1 = require("./simple-auth.service"); let SimpleAuthPlugin = SimpleAuthPlugin_1 = class SimpleAuthPlugin { constructor(conf, emailConf) { this.conf = conf; this.emailConf = emailConf; } onModuleInit() { this.registerEventHandler(); } onApplicationBootstrap() { this.cloneEmailTemplate(); } registerEventHandler() { const handlerExisted = this.emailConf.handlers.some(handler => handler.listener.type === constants_2.EMAIL_EVENT_NAME); if (handlerExisted) { return; } this.emailConf.handlers.push(email_handler_1.oneTimeCodeRequestedEventHandler); } cloneEmailTemplate() { /* eslint-disable @typescript-eslint/no-explicit-any */ const plugins = this.conf.plugins; const emailPlugin = plugins.find(plg => plg == email_plugin_1.EmailPlugin); if (emailPlugin) { const options = emailPlugin['options']; const templatePath = options.templatePath; (0, copy_dir_1.copyDir)(path_1.default.join(__dirname, './template/onetimecode-requested'), templatePath); core_1.Logger.info(`Template for onetimecode-requested created at ${templatePath}`, constants_2.SIMPLE_AUTH_PLUGIN_LOG_CONTEXT); } else { core_1.Logger.warn(`Cannot find EmailPlugin in Vendure Config. This pluginn might not work correctly."`, constants_2.SIMPLE_AUTH_PLUGIN_LOG_CONTEXT); } } static init(options) { SimpleAuthPlugin_1.options = { ...constants_2.DEFAULT_OPTIONS, ...options }; return SimpleAuthPlugin_1; } }; SimpleAuthPlugin.options = constants_2.DEFAULT_OPTIONS; SimpleAuthPlugin = SimpleAuthPlugin_1 = __decorate([ (0, core_1.VendurePlugin)({ imports: [ core_1.PluginCommonModule, common_1.CacheModule.registerAsync({ useFactory: () => { return SimpleAuthPlugin_1.options.cacheModuleOption; } }), email_plugin_1.EmailPlugin ], providers: [ simple_auth_service_1.SimpleAuthService, { provide: constants_2.SIMPLE_AUTH_PLUGIN_OPTIONS, useFactory: () => SimpleAuthPlugin_1.options } ], shopApiExtensions: { schema: schema_1.queryExtension, resolvers: [simple_auth_resolver_1.SimpleAuthResolver] }, configuration: (conf) => { const simpleAuthStrategy = new simple_auth_strategy_1.SimpleAuthStrategy(); conf.authOptions.shopAuthenticationStrategy.push(simpleAuthStrategy); return conf; }, }), __param(0, (0, common_1.Inject)(core_1.ConfigService)), __param(1, (0, common_1.Inject)(constants_1.EMAIL_PLUGIN_OPTIONS)), __metadata("design:paramtypes", [core_1.ConfigService, Object]) ], SimpleAuthPlugin); exports.SimpleAuthPlugin = SimpleAuthPlugin; //# sourceMappingURL=simple-auth.module.js.map