christmas/middlewares/verifyAuth.js

24 lines
778 B
JavaScript
Raw Normal View History

2022-10-03 08:03:02 -07:00
const ROUGHLY_ONE_YEAR_IN_MILLISECONDS = 1000 * 60 * 60 * 24 * 30 * 12
const COOKIE_NAME = 'christmas_community.guestpassword'
2018-11-20 11:19:58 -08:00
module.exports = options => {
return (req, res, next) => {
2020-11-08 13:54:08 -08:00
options = options || {}
2020-10-29 20:50:36 -07:00
let authed = false
try {
authed = req.isAuthenticated()
} catch {
return res.send('auth fail')
2018-11-20 11:19:58 -08:00
}
2020-10-29 20:50:36 -07:00
if (authed) return next()
2022-10-03 08:03:02 -07:00
if (_CC.config.guestPassword && (req.query.pw === _CC.config.guestPassword || req.cookies[COOKIE_NAME] === _CC.config.guestPassword)) {
2022-10-03 07:49:36 -07:00
req.user = {
_id: '_CCUNKNOWN'
}
2022-10-03 08:03:02 -07:00
res.cookie(COOKIE_NAME, _CC.config.guestPassword, { maxAge: ROUGHLY_ONE_YEAR_IN_MILLISECONDS })
2022-10-03 07:49:36 -07:00
return next()
}
2022-10-03 08:03:02 -07:00
res.redirect(options.failureRedirect || _CC.config.defaultFailureRedirect)
2020-11-08 13:54:08 -08:00
}
}