ServerCookies

Working with Cookies in feTS

HTTP cookies are small pieces of data used to maintain stateful information between the client and the server. They allow the server to recognize a client across multiple requests. To learn more about the concept of cookies, see the HTTP Cookies documentation on MDN.

In feTS, a plugin is provided for handling cookies in accordance with the web standard CookieStore. The plugin allows you to retrieve cookies from a client’s request and send cookies back to the client in your server’s response. Comprehensive details about the CookieStore API are available in the MDN documentation.

Installation

npm i @whatwg-node/server-plugin-cookies

How to Use Cookies in feTS

First, you need to import the necessary modules and create a router with the useCookies plugin:

cookies.ts
import { createRouter, Response } from 'fets'
import { useCookies } from '@whatwg-node/server-plugin-cookies'
 
const router = createRouter({
  plugins: [useCookies()]
})

Next, you can define routes. In this example, two routes are defined:

  1. A GET route at /me that checks if a session_id cookie is present, and if so, retrieves the user associated with that session. If the session_id cookie is not found, it responds with a 401 Unauthorized error.

    cookies.ts
    .route({
     path: '/me',
     method: 'GET',
     schemas: {/* ... */},
     handler: async request => {
      const sessionId = await request.cookieStore?.get('session_id')
      if (!sessionId) {
        return Response.json({ error: 'Unauthorized' }, { status: 401 })
      }
      const user = await getUserBySessionId(sessionId)
      return Response.json(user)
     }
    })
  2. A POST route at /login that logs a user in by creating a new session for them and setting the session_id cookie.

    cookies.ts
    .route({
      path: '/login',
      method: 'POST',
      handler: async request => {
        const { username, password } = await request.json()
        const sessionId = await createSessionForUser({ username, password })
        await request.cookieStore?.set('session_id', sessionId)
        return Response.json({ message: 'ok' })
      }
     })

This is a basic usage of the feTS cookies plugin. Depending on your application, you may need to implement more complex logic for handling cookies.