@denz93/vendure-plugin-3rd-party-auth
Version:
A set of auth strategies to work with 3rd party such as Google, Facebook, etc
55 lines (54 loc) • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleAuthStrategy = void 0;
const core_1 = require("@vendure/core");
const graphql_tag_1 = require("graphql-tag");
const constants_1 = require("./constants");
const interfaces_1 = require("./interfaces");
const google_auth_service_1 = require("./google-auth.service");
const cross_strategies_checker_service_1 = require("./cross-strategies-checker.service");
class GoogleAuthStrategy {
constructor() {
this.name = constants_1.GOOGLE_STRATEGY_NAME;
}
defineInputType() {
return (0, graphql_tag_1.gql)(`
input GoogleAuthInput {
token: String!
}
`);
}
async authenticate(ctx, data) {
try {
const payload = await this.googleService.verify(data.token);
let user = await this.externalAuthService.findCustomerUser(ctx, this.name, payload.email);
if (user) {
return user;
}
await this.crossStrategyChecker.check(ctx, payload.email, this.name);
user = await this.externalAuthService.createCustomerAndUser(ctx, {
emailAddress: payload.email,
externalIdentifier: payload.email,
strategy: this.name,
verified: payload.email_verified ?? false,
firstName: payload.given_name ?? '',
lastName: payload.family_name ?? ''
});
return user;
}
catch (err) {
if (err instanceof interfaces_1.UserExistedInAnotherStrategyError) {
return err.message;
}
if (typeof err === 'object' && err && 'message' in err && typeof err.message === 'string')
return err.message;
return 'Unknown error';
}
}
init(injector) {
this.googleService = injector.get(google_auth_service_1.GoogleAuthService);
this.externalAuthService = injector.get(core_1.ExternalAuthenticationService);
this.crossStrategyChecker = injector.get(cross_strategies_checker_service_1.CrossStrategiesChecker);
}
}
exports.GoogleAuthStrategy = GoogleAuthStrategy;