Overview
Sessions in Kit are:- Database-backed for horizontal scalability
- Automatically initialized via
SessionMiddleware - Request-scoped with thread-local storage
- Secure with HttpOnly, SameSite cookies
Accessing Session Data
Use thesession() and session_mut() functions to access session data:
Session API
Reading Data
Writing Data
Flash Messages
Flash data is available only for the next request, perfect for success/error messages:Session Configuration
Configure sessions in your.env file:
Session Middleware
TheSessionMiddleware is automatically registered in bootstrap.rs:
Sessions Table
Sessions are stored in thesessions database table:
Session Garbage Collection
Expired sessions are automatically cleaned up. The session lifetime is determined bySESSION_LIFETIME in your .env file.
Working with the Auth System
Sessions integrate seamlessly with Kit’s authentication system:Thread Safety
Sessions use thread-local storage to ensure each request has its own isolated session data. This means:- Session data is automatically scoped to the current request
- No race conditions between concurrent requests
- No need for explicit locking or synchronization