UNPKG

@denz93/vendure-plugin-simple-auth

Version:

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

75 lines 3.64 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.SimpleAuthResolver = exports.RequestOneTimeCodeError = void 0; const common_1 = require("@nestjs/common"); const graphql_1 = require("@nestjs/graphql"); const core_1 = require("@vendure/core"); const isemail_1 = require("isemail"); const constants_1 = require("./constants"); const events_1 = require("./events"); const simple_auth_service_1 = require("./simple-auth.service"); class EmailValidation { transform(value) { if ((0, isemail_1.validate)(value)) { return value.toLowerCase(); } throw new Error(`${value} is not a valid email`); } } class RequestOneTimeCodeError { constructor(message, errorCode) { this.message = message; this.errorCode = errorCode; this.__typename = 'RequestOneTimeCodeError'; console.log('runhere'); } } exports.RequestOneTimeCodeError = RequestOneTimeCodeError; let SimpleAuthResolver = class SimpleAuthResolver { constructor(service, eventBus, pluginOptions) { this.service = service; this.eventBus = eventBus; this.pluginOptions = pluginOptions; } async requestOneTimeCode(ctx, email) { if (this.pluginOptions.preventCrossStrategies) { const foundStrategy = await this.service.checkCrossStrategies(ctx, email); if (foundStrategy) return new RequestOneTimeCodeError(`Email already used with "${foundStrategy}" authentication`, 'CROSS_EMAIL_AUTHENTICATION'); } const value = await this.service.generateCode(email); const fiteredValue = this.pluginOptions.isDev ? value : 'A code sent to your email'; this.eventBus.publish(new events_1.OneTimeCodeRequestedEvent(value, email, ctx)); return { __typename: 'OneTimeCode', value: fiteredValue }; } }; __decorate([ (0, graphql_1.Query)(), __param(0, (0, core_1.Ctx)()), __param(1, (0, graphql_1.Args)('email', EmailValidation)), __metadata("design:type", Function), __metadata("design:paramtypes", [core_1.RequestContext, String]), __metadata("design:returntype", Promise) ], SimpleAuthResolver.prototype, "requestOneTimeCode", null); SimpleAuthResolver = __decorate([ (0, graphql_1.Resolver)(), __param(0, (0, common_1.Inject)(simple_auth_service_1.SimpleAuthService)), __param(1, (0, common_1.Inject)(core_1.EventBus)), __param(2, (0, common_1.Inject)(constants_1.SIMPLE_AUTH_PLUGIN_OPTIONS)), __metadata("design:paramtypes", [simple_auth_service_1.SimpleAuthService, core_1.EventBus, Object]) ], SimpleAuthResolver); exports.SimpleAuthResolver = SimpleAuthResolver; //# sourceMappingURL=simple-auth.resolver.js.map