UserActivateController.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const Path = require('path')
  2. const UserGetter = require('../../../../app/src/Features/User/UserGetter')
  3. const UserRegistrationHandler = require('../../../../app/src/Features/User/UserRegistrationHandler')
  4. const ErrorController = require('../../../../app/src/Features/Errors/ErrorController')
  5. module.exports = {
  6. registerNewUser(req, res, next) {
  7. res.render(Path.resolve(__dirname, '../views/user/register'))
  8. },
  9. register(req, res, next) {
  10. const { email } = req.body
  11. if (email == null || email === '') {
  12. return res.sendStatus(422) // Unprocessable Entity
  13. }
  14. UserRegistrationHandler.registerNewUserAndSendActivationEmail(
  15. email,
  16. (error, user, setNewPasswordUrl) => {
  17. if (error != null) {
  18. return next(error)
  19. }
  20. res.json({
  21. email: user.email,
  22. setNewPasswordUrl,
  23. })
  24. }
  25. )
  26. },
  27. activateAccountPage(req, res, next) {
  28. // An 'activation' is actually just a password reset on an account that
  29. // was set with a random password originally.
  30. if (req.query.user_id == null || req.query.token == null) {
  31. return ErrorController.notFound(req, res)
  32. }
  33. if (typeof req.query.user_id !== 'string') {
  34. return ErrorController.forbidden(req, res)
  35. }
  36. UserGetter.getUser(
  37. req.query.user_id,
  38. { email: 1, loginCount: 1 },
  39. (error, user) => {
  40. if (error != null) {
  41. return next(error)
  42. }
  43. if (!user) {
  44. return ErrorController.notFound(req, res)
  45. }
  46. if (user.loginCount > 0) {
  47. // Already seen this user, so account must be activate
  48. // This lets users keep clicking the 'activate' link in their email
  49. // as a way to log in which, if I know our users, they will.
  50. res.redirect(`/login`)
  51. } else {
  52. req.session.doLoginAfterPasswordReset = true
  53. res.render(Path.resolve(__dirname, '../views/user/activate'), {
  54. title: 'activate_account',
  55. email: user.email,
  56. token: req.query.token,
  57. })
  58. }
  59. }
  60. )
  61. },
  62. }