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.

50 lines
1.1 KiB

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