Secure authentication cookies
Set a session or authentication cookie with browser-access restrictions.
Express.js · Interface
CookieOptions is the options object used with Express's cookie-setting API. It configures attributes such as lifetime, scope, HTTPS-only delivery, client-side access, SameSite behavior, signing, and partitioned storage.
CookieOptions is the options object accepted by Express's cookie-setting API. Its properties control cookie scope, lifetime, transport security, browser access, signing, encoding, and related Set-Cookie attributes.
res.cookie(name, value, [options])| Name | Type | Description |
|---|---|---|
name | String | Cookie name. |
value | String | Object | Cookie value; an object is serialized as JSON. |
options | Object | undefined | Options for the Set-Cookie header. |
Sets cookie name to value.
const express = require('express');
const app = express();
app.get('/login', (req, res) => {
res.cookie('session', 'abc123', {
httpOnly: true,
secure: true,
sameSite: 'lax',
maxAge: 60 * 60 * 1000
});
res.sendStatus(204);
});
app.listen(3000);This route sets a one-hour cookie that is restricted from client-side JavaScript, sent over HTTPS, and subject to SameSite=Lax behavior.
Set a session or authentication cookie with browser-access restrictions.
Define how long a cookie remains valid.
Limit where and when a browser sends a cookie.
res.cookie()res.clearCookie()httpOnlysecuresameSitesignedpartitioned