@denz93/vendure-plugin-simple-auth
Version:
Allow customers login using email and verification code (One time login)
88 lines • 3.86 kB
JavaScript
;
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); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleAuthService = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@vendure/core");
const crypto_1 = __importDefault(require("crypto"));
const constants_1 = require("./constants");
let SimpleAuthService = class SimpleAuthService {
constructor(cache, options, configService, externalAuthService) {
this.cache = cache;
this.options = options;
this.configService = configService;
this.externalAuthService = externalAuthService;
this.prefix = 'simple-auth-service';
}
keyof(email) {
return `${this.prefix}:${email}`;
}
async generateCode(email) {
const ttl = this.options.ttl;
const key = this.keyof(email);
let code = await this.cache.get(key);
if (typeof code === 'string') {
return code;
}
const alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const digits = '0123456789';
const target = digits + (this.options.includeAlphabet ? alphabets : '');
const length = this.options.length;
code = '';
for (let i = 0; i < length; i++) {
const index = crypto_1.default.randomInt(0, target.length);
code += target[index];
}
await this.cache.set(key, code, ttl);
return code;
}
async verifyCode(email, code) {
const key = this.keyof(email);
const savedCode = await this.cache.get(key);
if (typeof savedCode === 'string' && code === savedCode) {
await this.cache.del(key);
return true;
}
return false;
}
getAllStrategyNames() {
return this.configService
.authOptions
.shopAuthenticationStrategy
.map(strategy => strategy.name)
.filter(name => name !== constants_1.STRATEGY_NAME);
}
async checkCrossStrategies(ctx, email) {
for (const strategyName of this.getAllStrategyNames()) {
const user = await this.externalAuthService.findCustomerUser(ctx, strategyName, email);
if (user)
return strategyName;
}
return null;
}
};
SimpleAuthService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(common_1.CACHE_MANAGER)),
__param(1, (0, common_1.Inject)(constants_1.SIMPLE_AUTH_PLUGIN_OPTIONS)),
__param(2, (0, common_1.Inject)(core_1.ConfigService)),
__param(3, (0, common_1.Inject)(core_1.ExternalAuthenticationService)),
__metadata("design:paramtypes", [Object, Object, core_1.ConfigService,
core_1.ExternalAuthenticationService])
], SimpleAuthService);
exports.SimpleAuthService = SimpleAuthService;
//# sourceMappingURL=simple-auth.service.js.map