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 :
- How To Insert Current Datetime In Database Laravel
- How To Calculate Distance Between Two Latitude And Longitude In Laravel
- Laravel 10 Firebase Web Push Notification Tutorial
- How to Generate PDF File Using DomPDF In Laravel 9 Example
- Arr::collapse() | Laravel Helper Function
- Laravel pwa Integration Tutorial
- How To File Upload AWS s3 Bucket In Laravel 9
- Laravel Delete Image From Storage Folder
- Error : You need to install the imagick extension to use this back end
- Laravel 9 Ajax Image Upload With Preview Tutorial