UnsupportedBrowserMiddleware.js 662 B

1234567891011121314151617181920212223
  1. const Bowser = require('bowser')
  2. const Settings = require('settings-sharelatex')
  3. function unsupportedBrowserMiddleware(req, res, next) {
  4. if (!Settings.unsupportedBrowsers) return next()
  5. const userAgent = req.headers['user-agent']
  6. const parser = Bowser.getParser(userAgent)
  7. // Allow bots through by only ignoring bots or unrecognised UA strings
  8. const isBot = parser.isPlatform('bot') || !parser.getBrowserName()
  9. if (isBot) return next()
  10. const isUnsupported = parser.satisfies(Settings.unsupportedBrowsers)
  11. if (isUnsupported) {
  12. return res.redirect('/unsupported-browser')
  13. }
  14. next()
  15. }
  16. module.exports = { unsupportedBrowserMiddleware }