overleaf/services/web/app/src/infrastructure/UnsupportedBrowserMiddleware.js
Jakob Ackermann 358e8b7424 Merge pull request #5349 from overleaf/jpa-no-depreacted-api
[misc] fix eslint violations for node/no-depreacted-api

GitOrigin-RevId: 0f7d64984da9e789c4ab95381db34afb89fa1a94
2021-10-21 08:03:18 +00:00

50 lines
1.3 KiB
JavaScript

const Bowser = require('bowser')
const Settings = require('@overleaf/settings')
const Url = require('url')
const { getSafeRedirectPath } = require('../Features/Helpers/UrlHelper')
function unsupportedBrowserMiddleware(req, res, next) {
if (!Settings.unsupportedBrowsers) return next()
// Prevent redirect loop
const path = req.path
if (path === '/unsupported-browser') return next()
const userAgent = req.headers['user-agent']
if (!userAgent) return next()
const parser = Bowser.getParser(userAgent)
// Allow bots through by only ignoring bots or unrecognised UA strings
const isBot = parser.isPlatform('bot') || !parser.getBrowserName()
if (isBot) return next()
const isUnsupported = parser.satisfies(Settings.unsupportedBrowsers)
if (isUnsupported) {
return res.redirect(
Url.format({
pathname: '/unsupported-browser',
query: { fromURL: req.originalUrl },
})
)
}
next()
}
function renderUnsupportedBrowserPage(req, res) {
let fromURL
if (typeof req.query.fromURL === 'string') {
try {
fromURL =
Settings.siteUrl + (getSafeRedirectPath(req.query.fromURL) || '/')
} catch (e) {}
}
res.render('general/unsupported-browser', { fromURL })
}
module.exports = {
renderUnsupportedBrowserPage,
unsupportedBrowserMiddleware,
}