Amir Zarrinkafsh 6628760c21
fix: root domain detection
The current solution does not support second-level domains or IP addresses, the jwt cookie cannot be set properly and a user will be unable to login.

This change allows user to publish Heimdall on IP addresses or second-level domains successfully.
2021-09-13 14:02:06 +10:00

28 lines
750 B
JavaScript

'use strict'
const passport = require('passport')
const getRootDomain = require('../src/utils/Helpers')
module.exports = {
authorize: (req, res, next) => {
passport.authorize('jwt', { session: false }, (err, user) => {
if (err) {
req.user = null
}
if (user && !req.originalUrl.match(/\/logout/)) {
// Extend the tokens life while the user is browsing
const token = user.generateJWT()
const domain = getRootDomain(req.protocol + '://' + req.hostname + req.originalUrl) // Set cookie on top level domain for auth proxying
res.cookie('jwt', token, {
domain: domain,
maxAge: 3600000
})
}
req.user = user
next()
})(req, res, next)
}
}