a2r
Version:
A2R Framework
38 lines (37 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const telemetry_1 = require("@a2r/telemetry");
const bcryptjs_1 = require("bcryptjs");
const dbPool_1 = require("../dbPool");
/**
* Log ins user
* @param email User email
* @param password User password
*/
const login = async (email, password) => {
try {
telemetry_1.out.verbose(`Login user: ${email} / ${password}`);
if (!email || !password) {
telemetry_1.out.error('Email and password are mandatory');
return { ok: false, error: 'Email and password are mandatory' };
}
const collection = await (0, dbPool_1.getCollection)('users');
const user = await collection.findOne({ _id: email }, { projection: { roles: 1 } });
if (!user) {
telemetry_1.out.error(`There is no user with email ${email}`);
return { ok: false, error: `There is no user with email ${email}` };
}
const check = await (0, bcryptjs_1.compare)(password, user.password);
if (!check) {
telemetry_1.out.error(`Password is incorrect`);
return { ok: false, error: 'Password is incorrect' };
}
const { _id, roles } = user;
return { ok: true, info: { _id, roles } };
}
catch (ex) {
telemetry_1.out.error(`Error at user login: ${ex.stack || ex.message}`);
return { ok: false, error: ex.stack || ex.message };
}
};
exports.default = login;