-
-
Save Trikolon/5ea7020e299090a0723734ed8dbc62d7 to your computer and use it in GitHub Desktop.
Express Cookie Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const cookieParser = require('cookie-parser'); | |
const app = express(); | |
const port = 3000; | |
app.use(cookieParser()); | |
app.get('/', (req, res) => { | |
const { sessionToken } = req.cookies; | |
if (!sessionToken) { | |
const cookieValue = Math.random().toString(); | |
console.debug('Setting new cookie', cookieValue); | |
res.cookie('sessionToken', cookieValue, { maxAge: 900000, httpOnly: true }); | |
res.status(200).send('Please log in!'); | |
return; | |
} | |
console.debug('Got cookie', sessionToken); | |
res.status(200).send('You are logged in!'); | |
}); | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment