Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@brightcove/hono-sessions

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@brightcove/hono-sessions

A session manager for Hono. Uses cookies and DynamoDB as the session storage backend

  • 1.0.3
  • unpublished
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Hono Sessions

package-info NPM NodeJS

A session manager for Hono. Uses cookies and DynamoDB as the session storage backend

Install

npm install @brightcove/hono-sessions --save

Usage

A middleware is provided that allows configuration of the session options and adds the object sessions to the Hono context.

import { Hono } from 'hono';
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { sessions, DynamoDBAdapter } from '@brightcove/hono-sessions';

const client = new DynamoDBClient({
    endpoint: 'http://localhost:4566',
    region: 'us-east-1'
});
const document = DynamoDBDocument.from(client);

const app = new Hono();

app.use(sessions({
    adapter: new DynamoDBAdapter({
        tableName: 'my-table',
        primaryKey: 'pk',
        sortKey: 'sk',
        expiresAttr: 'expires',
        document
    })
    name: 'session_storage',
    cookie: {
        maxAge: 60000,
        secure: true
    }
}));

app.get('/my_route', async (c, next) => {
    const session = c.get('session');
});

It supports custom database adapters that must conform to the exporting Adapter interface, but comes with a DynamoDBAdapter by default

interface Adapter {
    get: (key: Record<string, string>) => Promise<SessionItem | undefined>;
    set: (key: Record<string, string>, data: any, cookie: any, withExpires?: boolean) => Promise<void>;
    delete: (key: Record<string, string>) => Promise<void>;
    defaultKeyFn: () => (sessionId: string) => Record<string, string>;
}

Options

DynamoDBAdapter Options
ParamTypeDescriptionRequiredDefault
tableNamestringDynamoDB table nameyes
primaryKeystringDynamoDB primary keynopk
sortKeystringDynamoDB sort keynosk
expiresAttrstringDynamoDB TTL attribute name. This will be used for setting session expiration and auto expiration behaviornoexpires
documentDynamoDBDocumenthttps://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-lib-dynamodb/Class/DynamoDBDocumentyes

Middleware Options

ParamTypeDescriptionRequiredDefault
adapterAdapterA valid Adapter instanceyes
cookieobjectAccepts all the Hono cookie optionsyes
namestringThe session cookie namenosid.bgs
secretstringThe secret used for signing cookiesyes, if cookie.secure, otherwise no
loggerLoggerWhat will be used for logging errors (ie. logger.error()). console is used by default if not specifiednoconsole
allowOverwritebooleanDetermines whether a new session can be started when the current one hasn't been endednotrue

Starting a session

This creates the session item in the database, initialized with a serialized version of any data passed into the function (must be serializable or this will fail) and sets the session cookie on the response.

import { startSession } from '@brightcove/hono-sessions';

app.get('/my_route', async (c, next) => {
    await startSession(c, {
        user_id: 1234,
        name: 'user'
    });
    ...
});

Updating a session

The sessions object exposes both the session and cookie, which can freely be edited.

app.get('/my_route', async (c, next) => {
    const session = c.get('session');
    const cookie = c.get('sessionCookie');

    session.newField = 'new value';
    ...
});

If any of the updated cookie options are invalid, this will fail.

When the request is finalizing, if either has been updated the changes will automatically be synced back to the database.

If any of the cookie options were updated an updated cookie will be set in the response.

Ending a session

This deletes the session from the database and the session cookie in the response.

import { endSession } from '@brightcove/hono-sessions';

app.get('/my_route', async (c, next) => {
    await endSession(c);
    ...
});

FAQs

Package last updated on 30 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc