Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Adding Service Layer Implementation #164

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added: Separation of user password controller to user password servic…
…e with reset password
Yarin Podoler committed Aug 28, 2014
commit bba70c42608d83baba493a479fd4463de0e9867c
76 changes: 70 additions & 6 deletions app/services/users.password.server.service.js
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ exports.forgotPassword = function(username, hostname, callback){
},
email : {
to: user.email,
subject: 'Password Reset',
subject: 'Password Reset'
}
};
emailService.sendTemplate(options, function(err){
@@ -73,22 +73,86 @@ exports.forgotPassword = function(username, hostname, callback){
/**
* Service validation for a specific token
* @param token - {String}
* @param callback - {Function} in the form of function(err)
* @param callback - {Function} in the form of function(err, user)
* err - {Error}
* user - {User}
*/
exports.validateResetToken = function(token, callback){
function validateResetToken(token, callback){
User.findOne({
resetPasswordToken: token,
resetPasswordExpires: {
$gt: Date.now()
}
}, function(err, user) {
if (err){
return callback(err);
return callback(err, null);
}
if (!user) {
return callback(new Error('No user found'));
return callback(new Error('No user found'), null);
}

return callback(null);
return callback(null, user);
});
};
exports.validateResetToken = validateResetToken;

/**
* Service to reset a password according to a specified token
* @param token - {String} - the token of the reset request
* @param passwordDetails - {Object} in the form of:
* {
* newPassword : {String} the new password
* verifyPassword {String} the password again
* }
* @param callback - {Function} in the form of callback(err, user)
* err - {Error}
* user - {User}
*/
exports.resetPassword = function(token, passwordDetails, callback){
async.waterfall([

function(done) {
validateResetToken(token, function (err, user) {
done(err, user);
});
},
function(user, done){
if (passwordDetails.newPassword !== passwordDetails.verifyPassword) {
return callback(new Error('Passwords do not match'), null);
}

user.password = passwordDetails.newPassword;
user.resetPasswordToken = undefined;
user.resetPasswordExpires = undefined;

user.save(function(err) {
if (err) {
return callback(err, null);
}

done(err, user);
});
},
// send email
function(user, done){
var options = {
template : {
path : 'app/views/templates/reset-password-confirm-email.server.view.html',
renderOptions : {
name: user.displayName,
appName: config.app.title
}
},
email : {
to: user.email,
subject: 'Your password has been changed'
}
};
emailService.sendTemplate(options, function(err){
done(err, user);
});
}
], function(err, user) {
callback(err, user);
});
};