You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
855 B

  1. 'use strict'
  2. /* global appdata, rights */
  3. /**
  4. * Authentication middleware
  5. *
  6. * @param {Express Request} req Express Request object
  7. * @param {Express Response} res Express Response object
  8. * @param {Function} next Next callback function
  9. * @return {any} void
  10. */
  11. module.exports = (req, res, next) => {
  12. // Is user authenticated ?
  13. if (!req.isAuthenticated()) {
  14. if (req.app.locals.appconfig.public !== true) {
  15. return res.redirect('/login')
  16. } else {
  17. req.user = rights.guest
  18. res.locals.isGuest = true
  19. }
  20. } else {
  21. res.locals.isGuest = false
  22. }
  23. // Check permissions
  24. res.locals.rights = rights.check(req)
  25. if (!res.locals.rights.read) {
  26. return res.render('error-forbidden')
  27. }
  28. // Expose user data
  29. res.locals.user = req.user
  30. return next()
  31. }