@work-bee/session
@work-bee/session
Middleware for managing user sessions within the ServiceWorker.
Install
npm install @work-bee/session Usage
import { compileConfig, strategyCacheFirst } from "@work-bee/core";
import sessionMiddleware, {
getTokenAuthorization,
getExpiryJWT,
setTokenAuthorization,
} from "@work-bee/session";
const session = sessionMiddleware({
authnMethods: ["POST"],
authnPathPattern: //auth/login$/,
authnGetToken: getTokenAuthorization,
authnGetExpiry: getExpiryJWT,
authzPathPattern: //api//,
authzSetToken: setTokenAuthorization,
});
const config = compileConfig({
strategy: strategyCacheFirst,
middlewares: [session],
}); Options
| Option | Type | Default | Description |
|---|---|---|---|
authnMethods | string[] | — | HTTP methods for authentication requests |
authnPathPattern | RegExp | — | Pattern matching authentication endpoints |
authnGetToken | (response) => string | — | Extract auth token from response |
authnGetExpiry | (response, token) => number | — | Extract token expiry timestamp |
authzPathPattern | RegExp | — | Pattern matching endpoints that need authorization |
authzSetToken | (request, token) => Request | — | Attach auth token to outgoing request |
inactivityPromptEventType | string | — | Event type posted before session expiry |
postMessage | (message) => Promise<void> | — | Function to send messages to clients |
unauthnPathPattern | RegExp | — | Pattern matching logout/unauthenticate endpoints |
expiryEventType | string | — | Event type posted when session expires |
Returns
{ before, afterNetwork, after, activityEvent }
Helper Exports
import {
getTokenAuthorization,
getExpiryJWT,
getExpiryPaseto,
setTokenAuthorization,
} from "@work-bee/session"; getTokenAuthorization(response)— Extracts Bearer token from Authorization headergetExpiryJWT(response, token)— Extracts expiry from a JWT tokengetExpiryPaseto(response, token)— Extracts expiry from a PASETO tokensetTokenAuthorization(request, token)— Sets the Authorization Bearer header on a request