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

Commit

Permalink
Reset Password with better error handling and validation, Email from …
Browse files Browse the repository at this point in the history
…moved to environment settings
  • Loading branch information
pratik60 committed Jul 5, 2014
1 parent 9853bce commit 084e84c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
clientSecret: 'SECRET_KEY',
callbackURL: 'http://localhost:3000/auth/linkedin/callback'
},
emailFrom : 'SENDER EMAIL ADDRESS', // sender address like ABC <abc@example.com>
mailer: {
service: 'SERVICE_PROVIDER',
auth: {
Expand Down
1 change: 1 addition & 0 deletions config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
clientSecret: 'SECRET_KEY',
callbackURL: 'http://localhost:3000/auth/linkedin/callback'
},
emailFrom : 'SENDER EMAIL ADDRESS', // sender address like ABC <abc@example.com>
mailer: {
service: 'SERVICE_PROVIDER',
auth: {
Expand Down
13 changes: 7 additions & 6 deletions config/env/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ module.exports = {
clientSecret: 'SECRET_KEY',
callbackURL: 'http://localhost:3000/auth/linkedin/callback'
},
mailer: {
service: 'SERVICE_PROVIDER',
auth: {
user: 'EMAIL_ID',
pass: 'PASSWORD'
}
emailFrom : 'SENDER EMAIL ADDRESS', // sender address like ABC <abc@example.com>
mailer: {
service: 'SERVICE_PROVIDER',
auth: {
user: 'EMAIL_ID',
pass: 'PASSWORD'
}
}
};
8 changes: 6 additions & 2 deletions packages/users/public/controllers/meanUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ angular.module('mean.users')
$scope.resetpassword = function() {
$http.post('/reset/' + $stateParams.tokenId, {
password: $scope.user.password,
confirmPassword: $scope.user.confirmPassword
})
.success(function(response) {
$rootScope.user = response.user;
Expand All @@ -105,8 +106,11 @@ angular.module('mean.users')
$location.url('/');
}
})
.error(function(response) {
$scope.resetpassworderror = 'Could not update password as token is invalid or may have expired';
.error(function(error) {
if (error.msg === 'Token invalid or expired')
$scope.resetpassworderror = 'Could not update password as token is invalid or may have expired';
else
$scope.validationError = error;
});
};
}
Expand Down
27 changes: 17 additions & 10 deletions packages/users/public/views/reset-password.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<div data-ng-controller="ResetPasswordCtrl" class="reset-password">
<div class="alert alert-danger animated fadeIn" ng-show="resetpassworderror">{{resetpassworderror}}</div>
<h1>Enter your new password</h1>
<form ng-submit="resetpassword()" class="reset form-horizontal">
<label for="password" class="reset-password"> Enter your new password</label>
<div class="form-group enter-password">
<input id="password" type="password" name="password" class="form-control1" ng-model="user.password"/>
<div class="alert alert-danger animated fadeIn" ng-show="resetpassworderror">{{resetpassworderror}}</div>
<div ng-repeat="error in validationError">
<div class="alert alert-danger animated fadeIn">{{error.msg}}</div>
</div>
<div class="form-group save">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
<h1>Enter your new password</h1>
<form ng-submit="resetpassword()" class="reset form-horizontal">
<div class="form-group">
<label for="password" class="reset-password col-md-2"> Enter Password</label>
<input id="password" type="password" name="password" ng-model="user.password"/>
</div>
<div class="form-group">
<label for="confirmPassword" class="col-md-2">Repeat Password</label>
<input id="confirmPassword" type="password" name="confirmPassword" ng-model="user.confirmPassword"/>
</div>
<div class="form-group save">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>

17 changes: 12 additions & 5 deletions packages/users/server/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,20 @@ exports.user = function(req, res, next, id) {
exports.resetpassword = function(req, res, next) {
User.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function(err, user) {
if (err) {
return next(err);
return res.status(400).jsonp({msg: err});
}
if (!user) {
return res.status(500).jsonp({err: err});
return res.status(400).jsonp({msg: 'Token invalid or expired'});
}
req.assert('password', 'Password must be between 8-20 characters long').len(8, 20);
req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if (errors) {
return res.status(400).send(errors);
}
user.password = req.body.password;
// user.resetPasswordToken = undefined;
// user.resetPasswordExpires = undefined;
user.resetPasswordToken = undefined;
user.resetPasswordExpires = undefined;
user.save(function(err) {
req.logIn(user, function(err) {
if (err) return next(err);
Expand Down Expand Up @@ -185,7 +191,8 @@ exports.forgotpassword = function(req, res, next) {
},
function(token, user, done) {
var mailOptions = {
to: user.email
to: user.email,
from: config.emailFrom
};
mailOptions = templates.forgot_password_email(user, req, token, mailOptions);
config.sendMail(mailOptions);
Expand Down
1 change: 0 additions & 1 deletion packages/users/server/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
'http://' + req.headers.host + '/#!/reset/' + token + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n';
mailOptions.subject = 'Resetting the password';
mailOptions.from = 'SENDER EMAIL ADDRESS'; // sender address
return mailOptions;
}
};

0 comments on commit 084e84c

Please sign in to comment.