2
0

forgot-password.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const logger = require('@alias/logger')('growi:routes:forgot-password');
  2. const ApiResponse = require('../util/apiResponse');
  3. module.exports = function(crowi, app) {
  4. const PasswordResetOrder = crowi.model('PasswordResetOrder');
  5. const { appService, mailService, configManager } = crowi;
  6. const path = require('path');
  7. const actions = {};
  8. const api = {};
  9. actions.api = api;
  10. actions.forgotPassword = async function(req, res) {
  11. return res.render('forgot-password');
  12. };
  13. actions.resetPassword = async function(req, res) {
  14. return res.render('reset-password');
  15. };
  16. async function sendPasswordResetEmail(email, url, i18n) {
  17. return mailService.send({
  18. to: email,
  19. subject: 'Password Reset',
  20. template: path.join(crowi.localeDir, `${i18n}/notifications/passwordReset.txt`),
  21. // TODO: need to set appropriate values by GW-6828
  22. vars: {
  23. appTitle: appService.getAppTitle(),
  24. email,
  25. url,
  26. },
  27. });
  28. }
  29. api.post = async function(req, res) {
  30. const { email } = req.body;
  31. const grobalLang = configManager.getConfig('crowi', 'app:globalLang');
  32. const i18n = req.language || grobalLang;
  33. const appUrl = appService.getSiteUrl();
  34. try {
  35. const { passwordResetOrderData } = await PasswordResetOrder.createPasswordResetOrder(email);
  36. const url = new URL(`/forgot-password/token?${passwordResetOrderData.token}`, appUrl);
  37. const oneTimeUrl = url.href;
  38. await sendPasswordResetEmail(email, oneTimeUrl, i18n);
  39. return res.json(ApiResponse.success());
  40. }
  41. catch (err) {
  42. const msg = 'Error occurred during password reset request procedure';
  43. logger.error(err);
  44. return res.json(ApiResponse.error(msg));
  45. }
  46. };
  47. return actions;
  48. };