-1

I'm using Laravel 7.x and want to create a new search index for Algolia (with Laravel Scout). As I've come to learn I can achieve this with the command "php artisan scout:import". Laravel Scout as well as Passport are installed and I remember this used to work earlier.

Unfortuantely now, a few months later it doesn't work anymore, surely due to my fault changeing smth. in Auth or the like.

If I send "php artisan scout:import" I get the following error:

    ErrorException

  Declaration of 
  
  App\Http\Middleware\Authenticate::authenticate(array $guards) 
  
  should be compatible with 
  Illuminate\Auth\Middleware\Authenticate::authenticate($request, array $guards)

  at D:\laragon\www\myproject\app\Http\Middleware\Authenticate.php:55
    51|      * @return void
    52|      *
    53|      * @throws \Illuminate\Auth\AuthenticationException
    54|      */
  > 55|     protected function authenticate(array $guards)
    56|     {
    57|
    58|         if (empty($guards)) {
    59|             return $this->auth->authenticate();

  1   D:\laragon\www\myproject\app\Http\Middleware\Authenticate.php:10
      Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Declaration of App\Http\Middleware\Authenticate::authenticate(array $guards) should be compatible with Illuminate\Auth\Middleware\Authenticate::authenticate($request, array $guards)",
      
      "D:\laragon\www\myproject\app\Http\Middleware\Authenticate.php", [Object(Algolia\ScoutExtended\Console\Commands\ReImportCommand), 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")

I'd like to find the root of the issue but I don't understand the error:

Declaration of
App\Http\Middleware\Authenticate::authenticate(array $guards)

should be compatible with
Illuminate\Auth\Middleware\Authenticate::authenticate($request, array $guards)

Any hints/tipps where to start looking are very welcome.

UPDATE: Here's the method in Authenticate.php that might be the issue:

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);
}

the entire Authenticate.php is:

<?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Auth\Middleware\Authenticate as Middleware;
    use Illuminate\Auth\AuthenticationException;
    use Illuminate\Contracts\Auth\Factory as Auth;
    
    class Authenticate extends Middleware
    {
        /**
         * The authentication factory instance.
         *
         * @var \Illuminate\Contracts\Auth\Factory
         */
        protected $auth;
    
        /**
         * Create a new middleware instance.
         *
         * @param  \Illuminate\Contracts\Auth\Factory  $auth
         * @return void
         */
        public function __construct(Auth $auth)
        {
            $this->auth = $auth;
        }
    
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @param  string[]  ...$guards
         * @return mixed
         *
         * @throws \Illuminate\Auth\AuthenticationException
         */
        public function handle($request, Closure $next, ...$guards)
        {
            $this->authenticate($guards);
    
            return $next($request);
        }
    
        /**
         * Determine if the user is logged in to any of the given guards.
         *
         * @param  array  $guards
         * @return void
         *
         * @throws \Illuminate\Auth\AuthenticationException
         */
        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);
        }
    }
Dominic
  • 440
  • 8
  • 22
  • 1
    in your file App\Http\Middleware\Authenticate you have a method `authenticate()` that is not compatible with the default one, add that method to your question. you might have added a non optional parameter to it – N69S Jun 09 '21 at 11:49

1 Answers1

1

I think I found the problem there and the re-indexing worked, but the following solution messed with the other api calls in my project so after successful indexing I changed it back.

I leave this here as it might be useful for someone:

Steps: After I compared the method "authenticate()" in the 2 files

D:\laragon\www\myproject\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php and D:\laragon\www\myproject\app\Http\Middleware\Authenticate.php

I found they are not the same and therefore not compatible, so I changed the method in the Illuminate\ file to the same method as in the \app.. file.

Before:

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);
    }

After:

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);
    }

After that the error was gone and indexing worked.

Dominic
  • 440
  • 8
  • 22