Skip to content

StarPortal/cf-workers-session-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloudflare Workers Session Store

This package provides a Rails-like session store.

Installation

npm install @starportal/cf-workers-session-store

Usage

createCookieStore

Create a CookieStore middleware to provide data.session

# functions/_middleware.ts
import { createCookieStore } from '@starportal/cf-workers-session-store'

const sessionStore = async(ctx) => createCookieStore({ password: ctx.env.SESSION_PASSWORD })(ctx)
const userFinder = async({ data, next }) => {
  if(!data.session) {
    return next()
  }

  // Read from `Cookie: _user_id=1`
  const userID = await data.session.get('_user_id')

  // Do something...

  return next();
}

export const onRequest = [sessionStore, userFinder];

IStore

The interface implements put and get which have same interface as KVNamespace

// Write
const store = new CookieStore({ writer: request, reader: tmpResponse })
await store.put('_user_id', 1, ['HttpOnly', 'SameSite=Lax'])

// Read
await store.get('_user_id')

About

This package provides a Rails-like session store.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published