UserActivateController.js 1.4 KB

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