14

我正在使用 Laravel 5.5 并尝试为用户和管理员实现多重身份验证。当我尝试在浏览器中调用管理员登录表单时出现此错误。

错误 :

App\Exceptions\Handler::unauthenticated($request, App\Exceptions\AuthenticationException $exception) 的声明应该与 Illuminate\Foundation\Exceptions\Handler::unauthenticated($request, Illuminate\Auth\AuthenticationException $exception) 兼容

这是我未经身份验证的功能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));
    }

请帮我解决这个问题。

4

3 回答 3

41

您忘记use Illuminate\Auth\AuthenticationException在文件顶部添加

于 2017-09-21T09:37:11.663 回答
3

我正在使用 Laravel 7.X
而且我更喜欢在 Authenticate 中间件中这样做,
我这样做了,它对我来说效果很好。

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

    }

}
于 2020-04-02T04:48:49.860 回答
0

感谢您最近的回答 Thanh Nguyen。我的自定义身份验证中间件适用于最新版本

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

以前在 Handler.php 中使用未经身份验证的函数来替换父函数。

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

两者都可以工作,但是对于 array_get 的最新版本,获取我们使用的保护可能存在问题:

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

对于遇到此问题的任何人,此方法已被 Laravel 7.* 及更高版本弃用

于 2021-07-05T17:42:52.587 回答