a2r
Version:
A2R Framework
46 lines (45 loc) • 1.93 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const telemetry_1 = require("@a2r/telemetry");
const bcryptjs_1 = require("bcryptjs");
const dbPool_1 = require("../dbPool");
/**
* Registers user
* @param user User info
*/
const register = async (user) => {
try {
const { email, password: passwordInput } = user, userInfo = __rest(user, ["email", "password"]);
telemetry_1.out.verbose(`Registering user: ${email}`, userInfo);
if (!email || !passwordInput) {
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 userCheck = await collection.findOne({ _id: email }, { projection: { email: 1 } });
if (userCheck) {
telemetry_1.out.error(`Email ${email} already exists`);
return { ok: false, error: 'Email already exists' };
}
const password = await (0, bcryptjs_1.hash)(passwordInput, 12);
await collection.insertOne(Object.assign({ email,
password, _id: email, verified: false }, userInfo));
return { ok: true };
}
catch (ex) {
telemetry_1.out.error(`Error at user register: ${ex.stack || ex.message}`);
return { ok: false, error: ex.stack || ex.message };
}
};
exports.default = register;