UNPKG

@denz93/vendure-plugin-simple-auth

Version:

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

51 lines 1.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleAuthStrategy = void 0; const core_1 = require("@vendure/core"); const graphql_tag_1 = __importDefault(require("graphql-tag")); const isemail_1 = require("isemail"); const constants_1 = require("./constants"); const simple_auth_service_1 = require("./simple-auth.service"); class SimpleAuthStrategy { constructor() { this.name = constants_1.STRATEGY_NAME; } defineInputType() { return (0, graphql_tag_1.default) ` input SimpleAuthInput { email: String! code: String! } `; } async authenticate(ctx, data) { if (!(0, isemail_1.validate)(data.email)) { return "Email is invalid"; } const email = data.email.toLowerCase(); const isValidCode = await this.simpleAuthService.verifyCode(email, data.code); if (!isValidCode) return "Invalid verification code"; let user = await this.externalAuthenticationService.findCustomerUser(ctx, this.name, email); if (user) return user; user = await this.externalAuthenticationService.createCustomerAndUser(ctx, { emailAddress: data.email, externalIdentifier: data.email, strategy: this.name, verified: true, firstName: '', lastName: '', }); return user; } init(injector) { this.externalAuthenticationService = injector.get(core_1.ExternalAuthenticationService); this.simpleAuthService = injector.get(simple_auth_service_1.SimpleAuthService); } } exports.SimpleAuthStrategy = SimpleAuthStrategy; //# sourceMappingURL=simple-auth-strategy.js.map