Access the ctx.cookies object to read and write signed or unsigned cookies. This property is a Cookies instance from the cookies package, configured with the application's keys and the request's secure state.
Usage:
// Read a cookie
const userId = this.cookies.get('userId');
// Set an unsigned cookie
this.cookies.set('userId', '12345');
// Set a signed cookie (requires app.keys to be set)
this.cookies.set('userId', '12345', { signed: true });
// Set a cookie with options
this.cookies.set('userId', '12345', {
maxAge: 86400000, // 1 day
httpOnly: true
});
Configuration:
- The
keys option is automatically taken from this.app.keys. - The
secure option is automatically taken from this.request.secure.
You can also override the cookies instance by setting this.cookies directly, though this is rarely needed.
Sources: lib/context.js