forgot-password.js 1.8 KB

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