Skip to main content
Kit provides database-backed session management similar to Laravel. Sessions are automatically initialized and managed through middleware.

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 the session() 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

The SessionMiddleware is automatically registered in bootstrap.rs:

Sessions Table

Sessions are stored in the sessions database table:
This table is automatically created when you run migrations on a new Kit project.

Session Garbage Collection

Expired sessions are automatically cleaned up. The session lifetime is determined by SESSION_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