update-password-and-login.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. module.exports = {
  2. friendlyName: 'Update password and login',
  3. description: 'Finish the password recovery flow by setting the new password and '+
  4. 'logging in the requesting user, based on the authenticity of their token.',
  5. inputs: {
  6. password: {
  7. description: 'The new, unencrypted password.',
  8. example: 'abc123v2',
  9. required: true
  10. },
  11. token: {
  12. description: 'The password token that was generated by the `sendPasswordRecoveryEmail` endpoint.',
  13. example: 'gwa8gs8hgw9h2g9hg29hgwh9asdgh9q34$$$$$asdgasdggds',
  14. required: true
  15. }
  16. },
  17. exits: {
  18. invalidToken: {
  19. description: 'The provided password token is invalid, expired, or has already been used.',
  20. responseType: 'expired'
  21. }
  22. },
  23. fn: async function (inputs, exits) {
  24. if(!inputs.token) {
  25. throw 'invalidToken';
  26. }
  27. // Look up the user with this reset token.
  28. var userRecord = await User.findOne({ passwordResetToken: inputs.token });
  29. // If no such user exists, or their token is expired, bail.
  30. if (!userRecord || userRecord.passwordResetTokenExpiresAt <= Date.now()) {
  31. throw 'invalidToken';
  32. }
  33. // Hash the new password.
  34. var hashed = await sails.helpers.passwords.hashPassword(inputs.password);
  35. // Store the user's new password and clear their reset token so it can't be used again.
  36. await User.update({ id: userRecord.id }).set({
  37. password: hashed,
  38. passwordResetToken: '',
  39. passwordResetTokenExpiresAt: 0
  40. });
  41. // Log the user in.
  42. this.req.session.userId = userRecord.id;
  43. return exits.success();
  44. }
  45. };