Skip to main content
Kit provides Laravel-style session-based authentication out of the box. When you create a new project with kit new, it includes a complete authentication system with login, registration, and protected routes.

Overview

The authentication system includes:
  • Session-based auth with database-backed sessions
  • Secure password hashing using bcrypt
  • CSRF protection on all state-changing requests
  • Auth middleware for protecting routes
  • Guest middleware for login/register pages
  • Remember me functionality

Auth Facade

The Auth struct provides a simple API for authentication operations:

Getting the Current User

Kit provides two methods to retrieve the currently authenticated user:

Auth::user()

Returns the user as a trait object (Arc<dyn Authenticatable>):

Auth::user_as<T>()

Returns the user cast to your concrete User type:

Authenticatable Trait

Your User model must implement the Authenticatable trait to enable Auth::user(). This is already set up for you when you create a new Kit project:

User Provider

The UserProvider trait tells Kit how to fetch users from your database. A default DatabaseUserProvider is registered in bootstrap.rs:
Register the provider in bootstrap.rs:

Protecting Routes

Auth Middleware

Use AuthMiddleware to protect routes that require authentication:
The redirect_to method specifies where unauthenticated users should be redirected. For API routes, use AuthMiddleware::new() which returns a 401 status instead.

Guest Middleware

Use GuestMiddleware to protect routes that should only be accessible to guests (like login and register pages):

Authentication Controller

Here’s a typical authentication controller:

User Model

The generated User model includes helper methods for authentication:

Frontend Pages

Kit generates React/Inertia pages for authentication:

Login Page

Sessions

Sessions are automatically managed by the framework. See the Sessions documentation for details on how to work with session data directly.

CSRF Protection

All POST, PUT, PATCH, and DELETE requests are automatically protected against CSRF attacks. See the CSRF Protection documentation for details.

Security Features

Kit’s authentication system includes several security measures:
  • bcrypt password hashing with secure cost factor
  • HttpOnly session cookies to prevent XSS attacks
  • SameSite=Lax cookies to prevent CSRF attacks
  • Secure cookies in production (when SESSION_SECURE=true)
  • CSRF tokens validated on all state-changing requests
  • Constant-time token comparison to prevent timing attacks
  • Session regeneration on logout to prevent session fixation

Environment Configuration

Configure authentication behavior in your .env file:

Database Tables

Authentication requires two database tables, which are automatically created when you run migrations:

Users Table

Sessions Table