@lomray/microservice-users
Version:
Users microservice based on NodeJS & inverted json.
56 lines (52 loc) • 1.81 kB
JavaScript
;
var tslib = require('tslib');
var microserviceHelpers = require('@lomray/microservice-helpers');
var microserviceNodejsLib = require('@lomray/microservice-nodejs-lib');
var exceptionCode = require('../constants/exception-code.js');
/**
* Sign in user
*/
class SignIn {
/**
* @constructor
*/
constructor({ login, password, repository }) {
this.login = login;
this.password = password;
this.repository = repository;
}
/**
* Init service
*/
static init(params) {
return new SignIn(params);
}
/**
* Sign in user
*/
auth() {
return tslib.__awaiter(this, void 0, void 0, function* () {
const user = yield this.repository.findOne(Object.assign({}, (this.isEmail() ? { email: this.login } : { phone: this.login })), { relations: ['profile'], select: microserviceHelpers.EntityColumns(this.repository), withDeleted: true });
/**
* Verify if the user was deleted and can he restore the account
*/
yield this.repository.verifyDeleteAt(user);
if (!user || !this.repository.isValidPassword(user, this.password)) {
throw new microserviceNodejsLib.BaseException({
code: exceptionCode.LOGIN_PASSWORD_INCORRECT,
message: 'Login or password incorrect.',
status: 422,
});
}
return user;
});
}
/**
* Check login is email
* @protected
*/
isEmail() {
return /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(String(this.login).toLowerCase());
}
}
module.exports = SignIn;