Laravel 12 Authentication Tutorial: In this guide, we will implement authentication in Laravel 12 using Breeze and Sanctum. Authentication is a core feature of modern web applications, allowing users to securely register, login, and access protected resources.
Laravel Breeze provides a simple authentication system for web applications, while Sanctum is used for API authentication.
Step 1. Install Laravel 12
composer create-project laravel/laravel laravel-auth cd laravel-auth
Step 2. Install Laravel Breeze
composer require laravel/breeze --dev php artisan breeze:install npm install && npm run dev php artisan migrate
Breeze provides login, registration, password reset, and email verification features.
Step 3. Test Authentication
Run the server:
php artisan serve
Visit:
- /register
- /login
Step 4. Install Laravel Sanctum
composer require laravel/sanctum php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" php artisan migrate
Step 5. Configure Sanctum
Add Sanctum middleware in app/Http/Kernel.php:
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
],
Step 6. Create API Login
Route::post('/login', function (Request $request) {
$credentials = $request->only('email', 'password');
if (!Auth::attempt($credentials)) {
return response()->json(['message' => 'Invalid login'], 401);
}
$user = Auth::user();
$token = $user->createToken('api-token')->plainTextToken;
return response()->json(['token' => $token]);
});
Step 7. Protect Routes
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Conclusion: You have successfully implemented authentication in Laravel 12 using Breeze for frontend authentication and Sanctum for API security. This setup is widely used in real-world applications.
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- map() Method | Laravel Collection Example
- Create Custom Route File With New Service Provider Laravel 8 9
- Laravel 6 call function undefined str_slug() - fix
- Force Redirect to www From Non-www Using htaccess In Laravel
- Laravel 9 Sanctum API Authentication Tutorial
- Image Upload From Url Example
- Laravel 9 Ajax Image Upload With Preview Tutorial
- Laravel Postcodes
- Arr::forget() | Laravel Helper Function
- How To Filter In Laravel Query Example