Skip to content

Instantly share code, notes, and snippets.

@Trikolon
Created August 11, 2021 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trikolon/5ea7020e299090a0723734ed8dbc62d7 to your computer and use it in GitHub Desktop.
Save Trikolon/5ea7020e299090a0723734ed8dbc62d7 to your computer and use it in GitHub Desktop.
Express Cookie Test
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