Master Laravel Breeze: Your Quick Start to Secure Authentication #
Laravel Breeze is a simple, minimal, and open-source implementation of all of Laravel's authentication features, including login, registration, password reset, email verification, and password confirmation. It’s built on Tailwind CSS, providing a beautiful and functional starting point for your application's user interface.
This tutorial will guide you through installing, configuring, and understanding Laravel Breeze, helping you integrate robust authentication into your Laravel projects with ease.
What is Laravel Breeze? #
Laravel Breeze serves as a minimal and simple starter kit for implementing authentication in Laravel applications. It scaffolds the necessary controllers, routes, and views (or API endpoints) for common authentication tasks. Unlike Laravel Jetstream, Breeze offers a simpler, unopinionated approach, giving developers more control over the frontend stack and customization.
Key Features:
- Authentication: Login, registration, password reset, email verification, password confirmation.
- Frontend: Built with Tailwind CSS, offering Blade, Livewire, React, or Vue stacks.
- API Authentication: Out-of-the-box support for API authentication using Laravel Sanctum.
- Simplicity: Less boilerplate, easier to customize for bespoke needs.
Prerequisites #
Before we begin, ensure you have the following installed on your system:
- PHP: Version 8.1 or higher
- Composer: Latest version
- Node.js & npm: Latest LTS version (for compiling assets)
- Database: MySQL, PostgreSQL, SQLite, etc. (configured in
.env) - Laravel Installer (optional):
composer global require laravel/installer
Step 1: Create a New Laravel Project #
If you don't have an existing Laravel project, create one using Composer:
composer create-project laravel/laravel my-breeze-app
cd my-breeze-app
Step 2: Install Laravel Breeze #
Once your project is set up, install Laravel Breeze via Composer:
composer require laravel/breeze
Step 3: Install the Breeze Scaffolding #
After installing the Composer package, you can install the Breeze scaffolding using the breeze:install Artisan command. This command will publish the authentication views, routes, controllers, and other resources to your application.
When prompted, you can choose your preferred frontend stack. For this tutorial, we'll focus on the Blade stack, which is the default.
php artisan breeze:install
You'll be asked to choose your stack:
- Blade with Alpine.js (default)
- Livewire with Alpine.js
- React with Inertia.js
- Vue with Inertia.js
- API scaffolding
For a basic web application, press Enter to select the default "Blade with Alpine.js". You'll also be asked if you want Dark Mode support and if you'd like to install Pest for testing.
After the installation, you'll see a message prompting you to install NPM dependencies and run a build:
npm install
npm run dev
The npm run dev command will compile your frontend assets (CSS via Tailwind CSS and JavaScript via Vite). For production, you would run npm run build.
Step 4: Database Configuration and Migration #
Ensure your database connection is configured correctly in your .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_breeze_app
DB_USERNAME=root
DB_PASSWORD=
Once configured, run the database migrations to create the necessary tables, including the users table:
php artisan migrate
Step 5: Start the Development Server #
You can now start the Laravel development server:
php artisan serve
Visit http://127.0.0.1:8000 in your browser. You should now see "Log in" and "Register" links in the top right corner. Click "Register" to create a new user and test the authentication flow.
Understanding the Breeze Structure #
Laravel Breeze publishes several files and directories that are crucial for its functionality:
app/Http/Controllers/Auth/: Contains controllers for handling login, registration, password reset, email verification, and password confirmation.routes/auth.php: Defines all authentication-related routes (e.g.,/register,/login,/forgot-password). These routes are typically included inroutes/web.php.resources/views/auth/: Blade templates for all authentication views (login, register, forgot password, etc.).resources/views/components/: Reusable Blade components used across the authentication views and other parts of your application, likeauth-card.blade.php,input-error.blade.php, etc.resources/css/app.css&resources/js/app.js: Main CSS and JavaScript files for your application, integrating Tailwind CSS and Alpine.js.tailwind.config.js: Tailwind CSS configuration file, where you can customize your design system.vite.config.js: Vite configuration for compiling assets.database/migrations/: Includes thecreate_users_table.phpmigration, among others.
Customizing Laravel Breeze #
One of Breeze's strengths is its ease of customization.
1. Customizing Views
Since Breeze uses Blade templates, you can directly edit any file within resources/views/auth/ and resources/views/components/. For example, to change the login page layout:
- Open
resources/views/auth/login.blade.php. - Modify the HTML and Blade directives as needed.
You can also create new components or modify existing ones in resources/views/components/.
2. Customizing Authentication Logic
The core authentication logic resides in the app/Http/Controllers/Auth/ directory. For instance, to modify the registration process:
- Open
app/Http/Controllers/Auth/RegisteredUserController.php. - The
storemethod handles user creation. You can add custom validation rules, extra fields, or post-registration actions here.
Example: Adding a username field during registration
-
Migration: Add a
usernamecolumn to youruserstable.Schema::table('users', function (Blueprint $table) { $table->string('username')->unique()->after('name'); });Run
php artisan migrate:fresh --seed(or create a new migration) if you want to apply changes to an existing database, but be careful as it will wipe all data. -
View: Add a username input field to
resources/views/auth/register.blade.php.<!-- Add this after the Name input --> <div class="mt-4"> <x-input-label for="username" :value="__('Username')" /> <x-text-input id="username" class="block mt-1 w-full" type="text" name="username" :value="old('username')" required autocomplete="username" /> <x-input-error :messages="$errors->get('username')" class="mt-2" /> </div> -
Controller: Update
app/Http/Controllers/Auth/RegisteredUserController.php.// In the store method, update the validation rules and user creation logic $request->validate([ 'name' => ['required', 'string', 'max:255'], 'username' => ['required', 'string', 'max:255', 'unique:'.User::class], // Add username validation 'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class], 'password' => ['required', 'confirmed', Rules\Password::defaults()], ]); $user = User::create([ 'name' => $request->name, 'username' => $request->username, // Add username 'email' => $request->email, 'password' => Hash::make($request->password), ]);
3. Customizing Routes
Authentication routes are defined in routes/auth.php. You can modify existing routes or add new ones here. For example, if you want to change the URL path for registration from /register to /signup, you would edit routes/auth.php.
// In routes/auth.php
// Replace
Route::get('register', [RegisteredUserController::class, 'create'])->name('register');
Route::post('register', [RegisteredUserController::class, 'store']);
// With
Route::get('signup', [RegisteredUserController::class, 'create'])->name('signup');
Route::post('signup', [RegisteredUserController::class, 'store']);
Remember to also update any links referencing route('register') in your views.
Laravel Breeze with API (Sanctum) #
Laravel Breeze can also scaffold an API-only authentication backend using Laravel Sanctum, perfect for SPAs (Single Page Applications) or mobile applications.
To install Breeze with API scaffolding:
php artisan breeze:install --api
This command will:
- Install Laravel Sanctum.
- Generate API authentication routes in
routes/auth.php(for stateless API authentication via Sanctum tokens). - Remove frontend views and assets, providing only the backend API endpoints.
You'll need to run php artisan migrate afterwards.
For API authentication, you would typically make requests to /api/login, /api/register, etc., and then use the generated Sanctum tokens for subsequent authenticated requests.
Conclusion #
Laravel Breeze provides an excellent, lightweight starting point for implementing robust authentication in your Laravel applications. Its simplicity and flexibility make it ideal for developers who prefer more control over their frontend and authentication logic without the overhead of a full-featured starter kit like Jetstream.
By following this tutorial, you should now be equipped to install, configure, and customize Laravel Breeze to fit your project's specific needs, ensuring a secure and user-friendly authentication experience.
Next Steps:
- Explore more about Laravel Sanctum for advanced API authentication.
- Dive deeper into Tailwind CSS for styling your application.
- Learn about Laravel policies and gates for authorization.