UNPKG

@lomray/microservice-users

Version:

Users microservice based on NodeJS & inverted json.

62 lines (58 loc) 1.51 kB
'use strict'; var tslib = require('tslib'); var crypto = require('crypto'); /** * Abstract class for confirmation services */ class Abstract { /** * @constructor */ constructor(repository, context) { this.repository = repository; this.context = context; } /** * Generate confirmation code * @protected */ generateCode(min = 100000, max = 999999) { return String(crypto.randomInt(min, max)); } /** * Save code * @protected */ saveCode(login, code, expirationAt = null) { return this.repository.upsert({ login, code, // set default expiration = 1 hour if not passed expirationAt: expirationAt === null ? Abstract.getTimestamp() + 60 * 60 : expirationAt, }, ['login']); } /** * Verify confirmation code */ verifyCode(login, code) { return tslib.__awaiter(this, void 0, void 0, function* () { if (!login || !code) { return false; } const model = yield this.repository.findOne({ login, code }); if (!model) { return false; } yield this.repository.remove(model); return Abstract.getTimestamp() <= model.expirationAt; }); } /** * Get current timestamp * @protected */ static getTimestamp() { return Math.round(Date.now() / 1000); } } module.exports = Abstract;