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
TheAuth 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 theAuthenticatable trait to enable Auth::user(). This is already set up for you when you create a new Kit project:
User Provider
TheUserProvider trait tells Kit how to fetch users from your database. A default DatabaseUserProvider is registered in bootstrap.rs:
bootstrap.rs:
Protecting Routes
Auth Middleware
UseAuthMiddleware to protect routes that require authentication:
redirect_to method specifies where unauthenticated users should be redirected. For API routes, use AuthMiddleware::new() which returns a 401 status instead.
Guest Middleware
UseGuestMiddleware 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
| Column | Type | Description |
|---|---|---|
| id | BIGINT | Primary key |
| name | VARCHAR | User’s name |
| VARCHAR | Unique email | |
| password | VARCHAR | Hashed password |
| remember_token | VARCHAR | Remember me token |
| created_at | TIMESTAMP | Creation time |
| updated_at | TIMESTAMP | Last update time |
Sessions Table
| Column | Type | Description |
|---|---|---|
| id | VARCHAR | Session ID (primary key) |
| user_id | BIGINT | Associated user (nullable) |
| payload | TEXT | Session data (JSON) |
| csrf_token | VARCHAR | CSRF token |
| last_activity | TIMESTAMP | Last activity time |