Laravel 11 Authentication: A Complete Guide

zendbot
120
blog images
Laravel 11 provides a powerful authentication system that simplifies user login, registration, and authorization. With built-in authentication scaffolding, developers can quickly implement secure authentication while maintaining flexibility for customization. In this article, we will explore how authentication works in Laravel 11, covering both traditional session-based authentication and API authentication using Laravel Sanctum.

Setting Up Authentication in Laravel 11Laravel 11 comes with an easy-to-use authentication system. You can set it up using the following steps:
Step 1: Install Laravel 11Ensure you have Laravel 11 installed. If not, install it using Composer:

composer create-project laravel/laravel myApp

Step 2: Install Laravel Breeze (For Basic Authentication)Laravel Breeze provides lightweight authentication features, including login, registration, password reset, and email verification.

composer require laravel/breeze --dev
php artisan breeze:install
php artisan migrate
npm install && npm run dev

Breeze sets up authentication views, controllers, and routes automatically.
Step 3: Install Laravel Sanctum (For API Authentication)If you're building an API, Laravel Sanctum provides token-based authentication:
 

composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate

Add the Sanctum middleware to api middleware group in app/Http/Kernel.php:

'api' => [
    \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
    'throttle:api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

Finally, add HasApiTokens to the User model:

use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable {
    use HasApiTokens, HasFactory, Notifiable;
}

Customizing AuthenticationCustomizing Authentication RoutesLaravel Breeze automatically generates authentication routes, but you can customize them in routes/web.php:

Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', function () {
        return view('dashboard');
    });
});

Adding Middleware for Role-Based AuthenticationTo restrict access based on user roles, create a middleware:

php artisan make:middleware RoleMiddleware

Modify app/Http/Middleware/RoleMiddleware.php:

public function handle($request, Closure $next, $role)
{
    if (!auth()->user() || auth()->user()->role !== $role) {
        return redirect('/home');
    }
    return $next($request);
}

Register it in app/Http/Kernel.php:

protected $routeMiddleware = [
    'role' => \App\Http\Middleware\RoleMiddleware::class,
];

Apply it to routes:

Route::get('/admin', function () {
    return view('admin.dashboard');
})->middleware('role:admin');

ConclusionLaravel 11 makes authentication simple yet powerful. Whether you're building a traditional web application or an API, Laravel offers seamless authentication solutions. By utilizing Breeze for quick authentication setup and Sanctum for API authentication, developers can secure their applications with minimal effort.