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 | 2x 2x 5x 5x 4x 4x 3x 2x 2x 2x 1x 1x 2x 2x 2x | import { AuthenticationStrategy, ExternalAuthenticationService, Injector, RequestContext, User } from "@vendure/core";
import { DocumentNode } from "graphql";
import gql from "graphql-tag";
import { FACEBOOK_STRATEGY_NAME } from "./constants";
import { CrossStrategiesChecker } from "./cross-strategies-checker.service";
import { FacebookAuthService } from "./facebook-auth.service";
import { FacebookAuthData, UserExistedInAnotherStrategyError } from "./interfaces";
export class FacebookAuthStrategy implements AuthenticationStrategy {
name: string = FACEBOOK_STRATEGY_NAME;
private facebookAuthService: FacebookAuthService;
private externalAuthService: ExternalAuthenticationService;
private crossStrategyChecker: CrossStrategiesChecker
defineInputType(): DocumentNode {
return gql(`
input FacebookAuthInput {
token: String!
}
`)
}
async authenticate(ctx: RequestContext, data: FacebookAuthData): Promise<string | false | User> {
try {
const userInfo = await this.facebookAuthService.verify(data.token);
let user = await this.externalAuthService.findCustomerUser(ctx, this.name, userInfo.email)
if (user) return user
await this.crossStrategyChecker.check(ctx, userInfo.email, this.name)
user = await this.externalAuthService.createCustomerAndUser(ctx, {
strategy: this.name,
emailAddress: userInfo.email,
externalIdentifier: userInfo.email,
verified: false,
firstName: userInfo.first_name??'',
lastName: userInfo.last_name??''
})
return user
} catch (err) {
if (err instanceof UserExistedInAnotherStrategyError) {
return err.message
}
return String(err)
}
}
init(injector: Injector) {
this.facebookAuthService = injector.get(FacebookAuthService)
this.externalAuthService = injector.get(ExternalAuthenticationService)
this.crossStrategyChecker = injector.get(CrossStrategiesChecker)
}
} |