send-password-recovery-email.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. module.exports = {
  2. friendlyName: 'Send password recovery email',
  3. description: 'Send a password recovery notification to the user with the specified email address.',
  4. inputs: {
  5. emailAddress: {
  6. description: 'The email address of the alleged user who wants to recover their password.',
  7. example: 'rydahl@example.com',
  8. type: 'string',
  9. required: true
  10. }
  11. },
  12. exits: {
  13. success: {
  14. description: 'The email address might have matched a user in the database. (If so, a recovery email was sent.)'
  15. },
  16. },
  17. fn: async function (inputs, exits) {
  18. // Find the record for this user.
  19. // (Even if no such user exists, pretend it worked to discourage sniffing.)
  20. var userRecord = await User.findOne({ emailAddress: inputs.emailAddress });
  21. if (!userRecord) {
  22. return exits.success();
  23. }//•
  24. // Come up with a pseudorandom, probabilistically-unique token for use
  25. // in our password recovery email.
  26. var token = await sails.helpers.strings.random('url-friendly');
  27. // Store the token on the user record
  28. // (This allows us to look up the user when the link from the email is clicked.)
  29. await User.update({ id: userRecord.id }).set({
  30. passwordResetToken: token,
  31. passwordResetTokenExpiresAt: Date.now() + sails.config.custom.passwordResetTokenTTL,
  32. });
  33. // Send recovery email
  34. await sails.helpers.sendTemplateEmail.with({
  35. to: inputs.emailAddress,
  36. subject: 'Password reset instructions',
  37. template: 'email-reset-password',
  38. templateData: {
  39. fullName: userRecord.fullName,
  40. token: token
  41. }
  42. });
  43. return exits.success();
  44. }
  45. };