UserEmailsController.coffee 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. AuthenticationController = require('../Authentication/AuthenticationController')
  2. UserGetter = require("./UserGetter")
  3. UserUpdater = require("./UserUpdater")
  4. EmailHelper = require("../Helpers/EmailHelper")
  5. UserEmailsConfirmationHandler = require "./UserEmailsConfirmationHandler"
  6. logger = require("logger-sharelatex")
  7. Errors = require "../Errors/Errors"
  8. module.exports = UserEmailsController =
  9. list: (req, res, next) ->
  10. userId = AuthenticationController.getLoggedInUserId(req)
  11. UserGetter.getUserFullEmails userId, (error, fullEmails) ->
  12. return next(error) if error?
  13. res.json fullEmails
  14. add: (req, res, next) ->
  15. userId = AuthenticationController.getLoggedInUserId(req)
  16. email = EmailHelper.parseEmail(req.body.email)
  17. return res.sendStatus 422 unless email?
  18. UserUpdater.addEmailAddress userId, email, (error)->
  19. return next(error) if error?
  20. UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (err) ->
  21. return next(error) if error?
  22. res.sendStatus 204
  23. remove: (req, res, next) ->
  24. userId = AuthenticationController.getLoggedInUserId(req)
  25. email = EmailHelper.parseEmail(req.body.email)
  26. return res.sendStatus 422 unless email?
  27. UserUpdater.removeEmailAddress userId, email, (error)->
  28. return next(error) if error?
  29. res.sendStatus 200
  30. setDefault: (req, res, next) ->
  31. userId = AuthenticationController.getLoggedInUserId(req)
  32. email = EmailHelper.parseEmail(req.body.email)
  33. return res.sendStatus 422 unless email?
  34. UserUpdater.setDefaultEmailAddress userId, email, (error)->
  35. return next(error) if error?
  36. res.sendStatus 200
  37. showConfirm: (req, res, next) ->
  38. res.render 'user/confirm_email', {
  39. token: req.query.token,
  40. title: 'confirm_email'
  41. }
  42. confirm: (req, res, next) ->
  43. token = req.body.token
  44. if !token?
  45. return res.sendStatus 422
  46. UserEmailsConfirmationHandler.confirmEmailFromToken token, (error) ->
  47. if error?
  48. if error instanceof Errors.NotFoundError
  49. res.status(404).json({
  50. message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
  51. })
  52. else
  53. next(error)
  54. else
  55. res.sendStatus 200