rutilus-analytics-node-js
Version:
Provides a GUI web app that allows users to examine their data in detail. Includes CSV export functionality.
72 lines (62 loc) • 1.61 kB
JavaScript
/**
* Rutilus
*
* @homepage https://gmrutilus.github.io
* @license Apache-2.0
*/
/**
* Contains the route used to create a new account or modify an existing one
*/
const hash = require('../../lib/auth').hash;
/** @module */
module.exports = /** @param {KoaCtx} ctx */ async (ctx) => {
const {
errorHandler,
schemas,
logger,
} = ctx.res;
const {
email,
password,
} = ctx.request.body;
const setBody = function (created = 0, updated = 0, error = '') {
ctx.body = {
created,
updated,
error,
};
};
// No email provided
if (!email) {
setBody(0, 0, 'Email not specified');
}
// No password provided
else if (!password) {
setBody(0, 0, 'Password not specified');
}
// Email and password provided
else {
await new Promise((resolve) => {
hash(password, (err, newPasswordHash) => {
if (err) {
logger.error(err);
return;
}
const newAccount = new schemas.Accounts({
_id: email,
password: newPasswordHash,
});
newAccount.save((err) => {
if (err) {
setBody(0, 0, err);
errorHandler.dbErrorCatcher(err);
}
else {
setBody(1, 0);
}
resolve();
});
});
});
}
};