Guest User

Untitled

a guest
Apr 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. //Change user's pasword
  2. app.post('/change-password', function(req, res, next) {
  3. var User = app.models.user;
  4. if (!req.accessToken) return res.sendStatus(401);
  5. //verify passwords match
  6. if (!req.body.password || !req.body.confirmation ||
  7. req.body.password !== req.body.confirmation) {
  8. return res.sendStatus(400, new Error('Passwords do not match'));
  9. }
  10.  
  11. User.findById(req.accessToken.userId, function(err, user) {
  12. if (err) return res.sendStatus(404);
  13. user.hasPassword(req.body.oldPassword, function(err, isMatch) {
  14. if (!isMatch) {
  15. return res.sendStatus(401);
  16. } else {
  17. user.updateAttribute('password', req.body.password, function(err, user) {
  18. if (err) return res.sendStatus(404);
  19. console.log('> password change request processed successfully');
  20. res.status(200).json({msg: 'password change request processed successfully'});
  21. });
  22. }
  23. });
  24. });
  25. });
  26.  
  27. //Hash the plain password
  28. user.updateAttribute('password', User.hashPassword(req.body.password), function(err, user) {
  29. ...
  30. });
  31.  
  32. module.exports = function (MyUserModel) {
  33.  
  34. ...
  35.  
  36. MyUserModel.updatePassword = function (ctx, emailVerify, oldPassword, newPassword, cb) {
  37. var newErrMsg, newErr;
  38. try {
  39. this.findOne({where: {id: ctx.req.accessToken.userId, email: emailVerify}}, function (err, user) {
  40. if (err) {
  41. cb(err);
  42. } else if (!user) {
  43. newErrMsg = "No match between provided current logged user and email";
  44. newErr = new Error(newErrMsg);
  45. newErr.statusCode = 401;
  46. newErr.code = 'LOGIN_FAILED_EMAIL';
  47. cb(newErr);
  48. } else {
  49. user.hasPassword(oldPassword, function (err, isMatch) {
  50. if (isMatch) {
  51.  
  52. // TODO ...further verifications should be done here (e.g. non-empty new password, complex enough password etc.)...
  53.  
  54. user.updateAttributes({'password': newPassword}, function (err, instance) {
  55. if (err) {
  56. cb(err);
  57. } else {
  58. cb(null, true);
  59. }
  60. });
  61. } else {
  62. newErrMsg = 'User specified wrong current password !';
  63. newErr = new Error(newErrMsg);
  64. newErr.statusCode = 401;
  65. newErr.code = 'LOGIN_FAILED_PWD';
  66. return cb(newErr);
  67. }
  68. });
  69. }
  70. });
  71. } catch (err) {
  72. logger.error(err);
  73. cb(err);
  74. }
  75. };
  76.  
  77. MyUserModel.remoteMethod(
  78. 'updatePassword',
  79. {
  80. description: "Allows a logged user to change his/her password.",
  81. http: {verb: 'put'},
  82. accepts: [
  83. {arg: 'ctx', type: 'object', http: {source: 'context'}},
  84. {arg: 'emailVerify', type: 'string', required: true, description: "The user email, just for verification"},
  85. {arg: 'oldPassword', type: 'string', required: true, description: "The user old password"},
  86. {arg: 'newPassword', type: 'string', required: true, description: "The user NEW password"}
  87. ],
  88. returns: {arg: 'passwordChange', type: 'boolean'}
  89. }
  90. );
  91.  
  92. ...
  93. };
  94.  
  95. {
  96. "name": "MyUserModel",
  97. "base": "User",
  98.  
  99. ...
  100.  
  101. "acls": [
  102. ...
  103. {
  104. "comment":"allow authenticated users to change their password",
  105. "accessType": "EXECUTE",
  106. "property":"updatePassword",
  107. "principalType": "ROLE",
  108. "principalId": "$authenticated",
  109. "permission": "ALLOW"
  110. }
  111. ...
  112. ],
  113. ...
  114. }
Add Comment
Please, Sign In to add comment