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
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".Creates a new session record and returns the session ID.
Reads session data for the given session ID. Returns
null if not found.Updates session data for the given session ID.
Deletes session data for the given session ID.
Returns
A session storage object with methods to manage sessions.
Parses the session ID from the Cookie header and loads the session data.
Saves session data and returns the Set-Cookie header.
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:Related
- createCookieSessionStorage - Store sessions in cookies
- createMemorySessionStorage - In-memory sessions for testing
- Sessions and Cookies - Sessions guide