14

I am using Laravel 5.5 and trying to implement multi authentication for users and admin. I am getting this error when i try to call admin login form in browser.

Error :

Declaration of App\Exceptions\Handler::unauthenticated($request, App\Exceptions\AuthenticationException $exception) should be compatible with Illuminate\Foundation\Exceptions\Handler::unauthenticated($request, Illuminate\Auth\AuthenticationException $exception)

Here is my unauthenticated function in app/Exceptions/Handler:

 protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
        $guard = array_get($exception->guards(), 0);
        switch ($guard) {
            case 'admin':
                $login = 'admin.login';
                break;
            default:
                $login = 'login';
                break;
        }
        return redirect()->guest(route($login));
    }

Please help me to resolve this issue.

Robert
  • 5,703
  • 2
  • 31
  • 32
MA-2016
  • 653
  • 3
  • 10
  • 30

4 Answers4

43

You forgot to add use Illuminate\Auth\AuthenticationException at the top of your file

Nerea
  • 2,107
  • 2
  • 15
  • 14
3

I am using Laravel 7.X
And I prefer to do that in Authenticate middleware
I did it like bellow and It is working well for me.

namespace App\Http\Middleware;

use Closure;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Arr;

class Authenticate extends Middleware
{
    protected $guards = [];
     /**
     * 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->guards = $guards;

        return parent::handle($request, $next, ...$guards);
    }
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            if (Arr::first($this->guards) === 'admin') {
                return route('admin.login');
            }
            return route('trainee.login');
        }

    }

}
Thanh Nguyen
  • 390
  • 4
  • 6
0

Thank you for your recent answer Thanh Nguyen. My custom auth middleware is work with the latest version

 if (Arr::first($this->guards) === 'admin') {
       return route('admin.login');
  }
 return route('customer.login');

Previously using unauthenticated function inside Handler.php to replace the parent function.

 protected function unauthenticated($request, AuthenticationException $exception)
{
    $guard = Arr::get($exception->guards(), 0);
    switch ($guard) {
      case 'respondent':
      $login = 'respondents.login';
      break;
      case 'admin':
      $login = 'admin.login';
      break;
      default:
      $login = 'admin.login';
      break;
    }

    return $request->expectsJson()
                ? response()->json(['message' => $exception->getMessage()], 401)
                : redirect()->guest(route($login));
}

Both is working, but there was likely an issue for latest version on array_get to obtain guards we use:

$guard = array_get($exception->guards(), 0);

For anyone facing this issue, this method has been deprecated for Laravel 7.* and above

0

Error in Handler Class - Laravel

public function handle($request, Closure $next)

With public function handle($request, Closure $next, ...$auth)

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 03 '23 at 09:28