Skip to content

Instantly share code, notes, and snippets.

@marcellodesales
Created March 5, 2015 21:27
Show Gist options
  • Save marcellodesales/5220e537bed04a7b7231 to your computer and use it in GitHub Desktop.
Save marcellodesales/5220e537bed04a7b7231 to your computer and use it in GitHub Desktop.
// Confirm the new user by retrieving the password submitted.
User.confirmNewUser = function(id, code, submittedUser, next) {
console.log("Submitted User ID: " + id + "; Data to confirm: " + JSON.stringify(submittedUser));
// Find the user by ID submitted
// http://docs.strongloop.com/display/public/LB/Exposing+models+over+REST
User.findById(id, function (err, savedUser) {
if (err) {
throw err;
}
// Validate if the submitted code is correct from the user based on his/her email.
var savedMd5 = crypto.createHash('md5').update(savedUser.email).digest('hex').substring(0, 5);
console.log("INFO: Verifying if the code is correct: " + code + " = " + savedMd5);
if (savedMd5 !== code) {
console.log("ERROR: The confirmation code is incorrect: " + code + " != " + savedMd5);
throw new Error("ERROR: The confirmation code is incorrect: " + code + " != " + savedMd5);
}
console.log("Current User: ", savedUser.toObject());
savedUser.password = submittedUser.password;
// Save the instance
savedUser.save();
next();
});
};
Browse your REST API at http://0.0.0.0:3000/explorer
Web server listening at: http://0.0.0.0:3000/
Submitted User ID: 1; Data to confirm: {"password":"23234dsdsd34"}
INFO: Verifying if the code is correct: ee23 = 792a8
ERROR: The confirmation code is incorrect: ee23 != 792a8
/home/mdesales/dev/users-service/common/models/user.js:75
throw new Error("ERROR: The confirmation code is incorrect: " + code
^
Error: ERROR: The confirmation code is incorrect: ee23 != 792a8
at /home/mdesales/dev/users-service/common/models/user.js:75:16
at /home/mdesales/dev/users-service/node_modules/loopback-datasource-juggler/lib/dao.js:865:5
at /home/mdesales/dev/users-service/node_modules/loopback-datasource-juggler/lib/dao.js:840:7
at /home/mdesales/dev/users-service/node_modules/loopback-datasource-juggler/lib/connectors/memory.js:345:7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment