Home > Blockchain >  Laravel 8.66: Incompatible Authenticate() methods (App vs. Middleware)
Laravel 8.66: Incompatible Authenticate() methods (App vs. Middleware)

Time:08-31

I managed to mess with my Laravel Authenticate method in ..\app\Http\Middleware\Authenticate.php

If I try to reindex Algolia with the command

php artisan scout:import

I get the error:

 ErrorException                                                                                 
                                                                                                
Declaration of App\Http\Middleware\Authenticate::authenticate(array $guards) should be compatibl
are\Authenticate::authenticate($request, array $guards)                                         
                                                                                                
at D:\laragon\www\myproject\app\Http\Middleware\Authenticate.php:56                            
   52▕      *                                                                                   
   53▕      * @throws \Illuminate\Auth\AuthenticationException                                  
   54▕      */                                                                                  
   55▕                                                                                          
➜  56▕     protected function authenticate(array $guards)                                       
   57▕     {                                                                                    
   58▕                                                                                          
   59▕       if (empty($guards)) {                                                              
   60▕             return $this->auth->authenticate();                                          
                                                                                                
1   D:\laragon\www\myproject\app\Http\Middleware\Authenticate.php:10                           
    Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Declaration of App\Http\Middl
te(array $guards) should be compatible with Illuminate\Auth\Middleware\Authenticate::authenticat
D:\laragon\www\myproject\app\Http\Middleware\Authenticate.php", [Object(Algolia\ScoutExtended\C
, Object(Symfony\Component\Finder\Finder), Object(Symfony\Component\Finder\SplFileInfo)])       
                                                                                                
2   D:\laragon\www\myproject\vendor\algolia\scout-extended\src\Helpers\SearchableFinder.php:113
    require_once("D:\laragon\www\myproject\app\Http\Middleware\Authenticate.php")    

      

my ..\app\Http\Middleware\Authenticate.php method is this:

protected function authenticate(array $guards)
{

  if (empty($guards)) {
        return $this->auth->authenticate();
    }

    foreach ($guards as $guard) {
        if ($this->auth->guard($guard)->check()) {
            return $this->auth->shouldUse($guard);
        }else{
            return null;
        }
    }

    throw new AuthenticationException('Unauthenticated.', $guards);
}

and the method in ..\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php is this:

protected function authenticate($request, array $guards)
{
    if (empty($guards)) {
        $guards = [null];
    }

    foreach ($guards as $guard) {
        if ($this->auth->guard($guard)->check()) {
            return $this->auth->shouldUse($guard);
        }
    }

    $this->unauthenticated($request, $guards);
}

What can I do to make these two compatible again? Since they are not the same, I tried to overwrite one authenticate method with the other method, but that didn't work at all.

How can I go about solving this issue? Should I go find the factory version of these files and copy over the authenticate methods?

CodePudding user response:

You can't mix and match function parameters like that. Just add Request $request as the first parameter of your function, and it will auto-inject the request dependency and become compatible.

CodePudding user response:

Thanks to @kjoedion and @lagbox I managed to get it working:

The trick was to make the signature of the methods matching, without using "Request" in the /vendor/../Authenticate (so $request only):

..\app\Http\Middleware\Authenticate.php

protected function authenticate($request, array $guards)
{

  if (empty($guards)) {
        return $this->authenticate($request, $guards);
    }

    foreach ($guards as $guard) {
        if ($this->auth->guard($guard)->check()) {
            return $this->auth->shouldUse($guard);
        }else{
            return null;
        }
    }

    throw new AuthenticationException('Unauthenticated.', $guards);
}

in ..\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php:

protected function authenticate($request, array $guards)
{
    if (empty($guards)) {
        $guards = [null];
    }

    foreach ($guards as $guard) {
        if ($this->auth->guard($guard)->check()) {
            return $this->auth->shouldUse($guard);
        }
    }

    $this->unauthenticated($request, $guards);
}
  • Related