Laravel middleware not redirecting to custom login page

Laravel middleware not redirecting to custom login page

Laravel 11 middleware not redirecting to the custom login page

Prerequisites

Before proceeding, ensure the following are installed on your system:

  • Git
    Git is a version control system used to manage code changes.
    šŸ‘‰ Download Git and follow the installation instructions for your OS.

  • PHP (v7.3 or higher)
    Laravel requires PHP.
    Check your version using:

    php -v
  • Composer
    A dependency manager for PHP, used to install Laravel and its packages.
    šŸ‘‰ Download Composer and follow the installation steps for your OS.

  • Web Server
    While Laravel includes a built-in development server, it’s recommended to use Apache or Nginx for production.

  • Database
    Ensure a supported DBMS (MySQL, PostgreSQL, SQLite, etc.) is installed if your project uses a database.

Creating a Custom Authentication Middleware in Laravel

Step 1 – Create a Custom Middleware

Generate the middleware:

php artisan make:middleware CustomAuthenticate

Edit the generated file at app/Http/Middleware/CustomAuthenticate.php:

<?php namespace App\Http\Middleware; use Illuminate\Auth\Middleware\Authenticate as Middleware; class CustomAuthenticate extends Middleware { protected function redirectTo($request) { if (! $request->expectsJson()) { return route('login'); } } }

This custom middleware overrides the default redirect behavior to redirect unauthenticated users to the login route.

Step 2 – Register the Middleware

Open app/Http/Kernel.php and update the $routeMiddleware array:

protected $routeMiddleware = [ 'custom.auth' => \App\Http\Middleware\CustomAuthenticate::class, // ... other middleware ];

Step 3 – Define the Login Route and Controller

In routes/web.php, Define your login route:

use App\Http\Controllers\AuthController; Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');

Create AuthController If it doesn't exist:

php artisan make:controller AuthController

Then in app/Http/Controllers/AuthController.php:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class AuthController extends Controller { public function showLoginForm() { return view('auth.login'); } }

Create a Blade view at resources/views/auth/login.blade.php With your custom login form HTML.

Step 4 – Protect Routes with Custom Middleware

Group your protected routes using the custom.auth middleware in routes/web.php:

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

Step 5 – Test the Setup

  1. Visit a protected route (like /dashboard) while logged out.

  2. You should be redirected to /login.

  3. After login, you should return to the originally requested page.

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close