Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 3x 3x 5x 1x 4x 4x 4x 2x 2x 1x 1x 3x 3x | import { AuthenticationStrategy, ExternalAuthenticationService, Injector, RequestContext, User } from "@vendure/core"; import { DocumentNode } from "graphql"; import gql from 'graphql-tag'; import { validate as isEmail } from "isemail"; import { STRATEGY_NAME } from "./constants"; import { SimpleAuthService } from "./simple-auth.service"; export type SimpleAuthData = { email: string code: string }; export class SimpleAuthStrategy implements AuthenticationStrategy<SimpleAuthData> { name = STRATEGY_NAME; simpleAuthService: SimpleAuthService; externalAuthenticationService: ExternalAuthenticationService; defineInputType(): DocumentNode { return gql` input SimpleAuthInput { email: String! code: String! } `; } async authenticate(ctx: RequestContext, data: SimpleAuthData): Promise<string | false | User> { if (!isEmail(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: Injector) { this.externalAuthenticationService = injector.get(ExternalAuthenticationService); this.simpleAuthService = injector.get(SimpleAuthService); } } |