2021-06-21 10:02:43 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const OError = require('@overleaf/o-error')
|
|
|
|
const AnalyticsRegistrationSourceHelper = require('./AnalyticsRegistrationSourceHelper')
|
2021-07-28 04:51:20 -04:00
|
|
|
const SessionManager = require('../../Features/Authentication/SessionManager')
|
2021-06-21 10:02:43 -04:00
|
|
|
|
2021-06-10 04:04:11 -04:00
|
|
|
function setSource(source) {
|
|
|
|
return function (req, res, next) {
|
|
|
|
if (req.session) {
|
|
|
|
req.session.required_login_for = source
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearSource() {
|
|
|
|
return function (req, res, next) {
|
2021-06-21 10:02:43 -04:00
|
|
|
AnalyticsRegistrationSourceHelper.clearSource(req.session)
|
2021-06-10 04:04:11 -04:00
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:02:43 -04:00
|
|
|
function setInbound() {
|
|
|
|
return function setInbound(req, res, next) {
|
|
|
|
if (req.session.inbound) {
|
|
|
|
return next() // don't overwrite referrer
|
|
|
|
}
|
|
|
|
|
2021-07-28 04:51:20 -04:00
|
|
|
if (SessionManager.isUserLoggedIn(req.session)) {
|
2021-06-21 10:02:43 -04:00
|
|
|
return next() // don't store referrer if user is alread logged in
|
|
|
|
}
|
|
|
|
|
|
|
|
const referrer = req.header('referrer')
|
|
|
|
try {
|
|
|
|
AnalyticsRegistrationSourceHelper.setInbound(
|
|
|
|
req.session,
|
|
|
|
req.url,
|
|
|
|
req.query,
|
|
|
|
referrer
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
// log errors and fail silently
|
|
|
|
OError.tag(error, 'failed to parse inbound referrer', {
|
|
|
|
referrer,
|
|
|
|
})
|
|
|
|
logger.warn({ error }, error.message)
|
|
|
|
}
|
|
|
|
next()
|
2021-06-10 04:04:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
setSource,
|
|
|
|
clearSource,
|
2021-06-21 10:02:43 -04:00
|
|
|
setInbound,
|
2021-06-10 04:04:11 -04:00
|
|
|
}
|