Home > Software engineering >  How to solve Middleware authentication error? [duplicate]
How to solve Middleware authentication error? [duplicate]

Time:10-07

When I try to login or register or try to access a page that is restricted this error shows. How to solve this error?

Declaration of App\Http\Middleware\Authenticate::handle($request, Closure $next) must be compatible with Illuminate\Auth\Middleware\Authenticate::handle($request, Closure $next, ...$guards)

This is the /app/Http/Middleware/Authenticate.php code:

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Closure;
use Auth;
class Authenticate extends  Middleware
{
    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            return $next($request);
        }
        return redirect()->route('user.login');
    }

}

CodePudding user response:

The original method's prototype (Middleware::handle()) is handle($request, Closure $next, ...$guards), so yours must be like that too. If you don't need those $guards, you may not use them, but they have to be in the prototype.

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Closure;
use Auth;
class Authenticate extends  Middleware
{
    public function handle($request, Closure $next, ...$guards)
    {
        if (Auth::check()) {
            return $next($request);
        }
        return redirect()->route('user.login');
    }

}
  • Related