Skip to content

Cookies ​

Historical documentation

This page describes Envuso 2.0. Its examples and current security or runtime compatibility have not been verified.

This documentation applies for requests and responses

All cookies for a request and response are stored in a "CookieJar" class instance.

So all methods below will apply to both requests and responses.

By default, all cookie values sent on a response are encrypted/hashed and this is then verified when the server reads the cookies If the cookie has been tampered with, it will be discarded.

typescript
// Request cookie jar:
request().cookieJar();
// Response cookie jar:
response().cookieJar();

The following would add a cookie to our response when it's sent

typescript
response().cookieJar().put('name', 'value')

Getting all cookies ​

typescript
request().cookieJar().all(); // returns CookieContract[]
typescript
request().cookieJar().get('name'); // returns CookieContract

Check if exists ​

typescript
request().cookieJar().has('name'); // returns boolean

When we get or create a cookie we will be interacting with the "Cookie" class

typescript
Cookie.create('name', 'value');
typescript
const cookie = Cookie.create('name', 'value');

cookie.withName('new name')
cookie.withValue('new value')
cookie.withMaxAge(DateTime.now().addDays(7))
cookie.withExpiresAt(DateTime.now().addDays(7))
cookie.withDomain('https://google.com')
cookie.withPath('/api')
cookie.withHttpOnly(true)
cookie.withSecure(true)
cookie.withSameSite(true)

Getting header string ​

typescript
Cookie.create('name', 'value').toHeaderString();

When reading a cookie from the request, we will only have it's key/value

typescript
const cookie = request().cookieJar().get('name')

cookie.getName();
cookie.getValue();

/Config/SessionConfiguration.ts some of the default cookie values are stored here under cookie

An independent open-source development effort.