Mock Config logoMock config
Mocking responses

Cookies

Set and clear response cookies from a resolver.

Response cookies let a route persist state on the client, such as an auth token or a locale. Inside a resolver you manage them with setCookie and clearCookie — no manual Set-Cookie header handling required.

Use setCookie to attach a cookie to the response.

rest.post('/login', ({ setCookie }) => {
  setCookie('auth', 'token');
  return { ok: true };
});

Every POST /login request responds with the auth cookie set on the client.

setCookie accepts an options object to control path, expiration, domain, and flags like httpOnly and secure.

rest.post('/login', ({ setCookie }) => {
  setCookie('auth', 'token', {
    httpOnly: true,
    secure: true,
    maxAge: 3600,
    path: '/'
  });

  return { ok: true };
});

Use clearCookie to remove a cookie from the client.

rest.post('/logout', ({ clearCookie }) => {
  clearCookie('auth');
  return { ok: true };
});

Cookies sent by the client are available through getCookie. This pairs naturally with setting one — read the incoming session, then respond accordingly.

rest.get('/profile', ({ getCookie }) => {
  const token = getCookie('token');
  if (!token) return { error: 'Unauthorized' };
  return { ok: true };
});

On this page