Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
profile hash: add on register, add on login if not there, update on p…
Browse files Browse the repository at this point in the history
…word reset
  • Loading branch information
Jeddf committed Dec 13, 2019
1 parent bed58e0 commit 1cfc28b
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions users.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var moment = require('moment');
var pg = require('pg');
var crypto = require('crypto');

var profileUtils = require('./lib/profile-utils');

module.exports = function (options) {
var seneca = this;
var plugin = 'cd-users';
Expand All @@ -30,6 +32,7 @@ module.exports = function (options) {
seneca.add({role: plugin, cmd: 'load_champions_for_user'}, cmd_load_champions_for_user);
seneca.add({role: plugin, cmd: 'load_dojo_admins_for_user'}, cmd_load_dojo_admins_for_user);
seneca.add({role: plugin, cmd: 'record_login'}, cmd_record_login);
seneca.add({role: plugin, cmd: 'update_profile_password'}, cmd_update_profile_password);
seneca.add({role: 'user', cmd: 'login'}, cmd_login);
seneca.add({role: 'user', cmd: 'cdf_login'}, cmd_cdf_login);
seneca.add({role: plugin, cmd: 'load_prev_founder'}, cmd_load_prev_founder);
Expand Down Expand Up @@ -65,6 +68,13 @@ module.exports = function (options) {
});
}

function cmd_update_profile_password (args, done) {
profileUtils.encodePassword(args.password).then((profileHash) => {
const updatedUser = Object.assign({}, args.user, {profilePassword: profileHash});
seneca.act({role: plugin, cmd: 'update'}, { id: args.user.id, user: updatedUser }, done);
});
}

function cmd_load (args, done) {
var seneca = this;
var id = args.id;
Expand Down Expand Up @@ -130,6 +140,13 @@ module.exports = function (options) {
}
};

function addProfilePassword (data, done) {
profileUtils.encodePassword(user.password).then((profileHash) => {
user.profilePassword = profileHash;
done(null, data);
});
}

function verifyCaptcha (done) {
request.post(postData, function (err, response, body) {
if (err) {
Expand Down Expand Up @@ -221,6 +238,7 @@ module.exports = function (options) {
async.waterfall([
verifyCaptcha,
checkPermissions,
addProfilePassword,
registerUser,
sendWelcomeEmail
], function (err, results) {
Expand Down Expand Up @@ -428,6 +446,8 @@ module.exports = function (options) {
out.reset = reset;
if (!out.ok) { return done(null, out); }

seneca.act({role: plugin, cmd: 'update_profile_password'}, {password: args.password, user: user});

reset.active = false;
reset.save$(function (err, reset) {
if (err) { return done(err); }
Expand Down Expand Up @@ -503,17 +523,28 @@ module.exports = function (options) {
if (err) return done(err);
if (!loginResponse.ok || !loginResponse.user) return done(null, loginResponse);

async.series([
const handlers = [
verifyPermissions,
recordLogin
], function (err) {
];

if (!loginResponse.user.profilePassword) {
handlers.push(updateProfilePassword);
}

async.series(handlers, function (err) {
if (err) {
return done(err);
}

return done(null, loginResponse);
});

function updateProfilePassword (next) {
seneca.act({role: plugin, cmd: 'update_profile_password'}, {password: args.password, user: loginResponse.user});
next();
}

function verifyPermissions (next) {
var userRole;

Expand Down

0 comments on commit 1cfc28b

Please sign in to comment.