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.
Cookie | CookieOptions
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
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