Skip to main content

createSessionStorage

Creates a SessionStorage object using a custom storage strategy. This is a low-level API for building session storage with your own backend (database, Redis, etc.).

Signature

SessionIdStorageStrategy
required
A storage strategy object that defines how session data is stored and retrieved.
The cookie used to store the session ID, or options to create one automatically. Defaults to a cookie named "__session".
function
required
Creates a new session record and returns the session ID.
function
required
Reads session data for the given session ID. Returns null if not found.
function
required
Updates session data for the given session ID.
function
required
Deletes session data for the given session ID.

Returns

object
A session storage object with methods to manage sessions.
function
Parses the session ID from the Cookie header and loads the session data.
function
Saves session data and returns the Set-Cookie header.
function
Deletes session data and returns a Set-Cookie header that clears the cookie.

Database Session Storage Example

Create session storage backed by a PostgreSQL database:
filename=app/sessions.server.ts

Redis Session Storage Example

filename=app/sessions.server.ts

Using Session Storage

Once created, use the session storage in your routes:
filename=app/routes/login.tsx

Reading Session Data

filename=app/routes/dashboard.tsx

Destroying Sessions

filename=app/routes/logout.tsx

TypeScript Support

Define session data types for full type safety:

Security Considerations

Always Sign Session Cookies

Use secrets to prevent session hijacking:

Implement Session Expiration

Always set and check expiration dates:

Generate Secure Session IDs

Use cryptographically secure random IDs:

Clean Up Expired Sessions

Implement periodic cleanup to prevent storage bloat: