add cookie

This commit is contained in:
Wingy 2022-10-03 11:03:02 -04:00
parent ad02bc3714
commit a998928202
4 changed files with 13 additions and 8 deletions

View file

@ -1,4 +1,6 @@
const config = require('../config') const ROUGHLY_ONE_YEAR_IN_MILLISECONDS = 1000 * 60 * 60 * 24 * 30 * 12
const COOKIE_NAME = 'christmas_community.guestpassword'
module.exports = options => { module.exports = options => {
return (req, res, next) => { return (req, res, next) => {
options = options || {} options = options || {}
@ -9,12 +11,13 @@ module.exports = options => {
return res.send('auth fail') return res.send('auth fail')
} }
if (authed) return next() if (authed) return next()
if (_CC.config.guestPassword && req.query.pw === _CC.config.guestPassword) { if (_CC.config.guestPassword && (req.query.pw === _CC.config.guestPassword || req.cookies[COOKIE_NAME] === _CC.config.guestPassword)) {
req.user = { req.user = {
_id: '_CCUNKNOWN' _id: '_CCUNKNOWN'
} }
res.cookie(COOKIE_NAME, _CC.config.guestPassword, { maxAge: ROUGHLY_ONE_YEAR_IN_MILLISECONDS })
return next() return next()
} }
res.redirect(options.failureRedirect || config.defaultFailureRedirect) res.redirect(options.failureRedirect || _CC.config.defaultFailureRedirect)
} }
} }

View file

@ -22,6 +22,7 @@
"chalk": "^4.1.2", "chalk": "^4.1.2",
"child-process-promise": "^2.2.1", "child-process-promise": "^2.2.1",
"connect-flash": "^0.1.1", "connect-flash": "^0.1.1",
"cookie-parser": "^1.4.6",
"dompurify": "^2.3.1", "dompurify": "^2.3.1",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",
"express": "4.17.1", "express": "4.17.1",

View file

@ -29,6 +29,7 @@ module.exports = ({ db, config }) => {
const router = express.Router() const router = express.Router()
router.use('/', express.static(path.join(__dirname, '../static'))) router.use('/', express.static(path.join(__dirname, '../static')))
router.use(require('cookie-parser')())
router.get('/', router.get('/',
async (req, res, next) => { async (req, res, next) => {

View file

@ -743,12 +743,12 @@
"resolved" "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz" "resolved" "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz"
"version" "1.0.4" "version" "1.0.4"
"cookie-parser@^1.4.3": "cookie-parser@^1.4.3", "cookie-parser@^1.4.6":
"integrity" "sha512-f13bPUj/gG/5mDr+xLmSxxDsB9DQiTIfhJS/sqjrmfAWiAN+x2O4i/XguTL9yDZ+/IFDanJ+5x7hC4CXT9Tdzw==" "integrity" "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA=="
"resolved" "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.5.tgz" "resolved" "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz"
"version" "1.4.5" "version" "1.4.6"
dependencies: dependencies:
"cookie" "0.4.0" "cookie" "0.4.1"
"cookie-signature" "1.0.6" "cookie-signature" "1.0.6"
"cookie-signature@1.0.6": "cookie-signature@1.0.6":