【问题标题】:Custom Guard Authentication Exception自定义 Guard 身份验证异常
【发布时间】:2019-09-18 00:16:51
【问题描述】:

我在我的应用程序中创建了一个自定义守卫(管理员),按照本教程在线 (https://pusher.com/tutorials/multiple-authentication-guards-laravel) 一切看起来都很好。唯一的问题是当我尝试访问受管理员保护的视图时,而不是给我一个异常 NotAuthenticate,而是给我一个“InvalidArgumentException Route [login] 未定义。”。

我的仪表板控制器自定义守卫是:

 public function __construct()
    {
        $this->middleware('auth:admin');
    }

但我无法理解发生了什么奇怪的事情是,当我在 web.php 中添加“Auth::routes();”路由时,它工作正常,异常 NotAuthenticate 被触发。

我添加 Auth::routes() 时,我的自定义仅适用于预期是否有原因?

管理员登录控制器:

namespace App\Http\Controllers\Admin\Auth;

use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Validation\ValidationException;

class LoginController extends Controller
{
     use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin/dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest:admin')->except('logout');
    }

    public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }


   public function showLoginForm()
    {
        return view('admin.auth.login');
    }



    /**
     * Log the user out of the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {
        $this->guard()->logout();

        $request->session()->invalidate();

        return $this->loggedOut($request) ?: redirect('/admin');
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('admin');
    }

}

【问题讨论】:

  • 我遇到了同样的问题。能否请您显示您的登录方法。 AdminLoginController 扩展了什么控制器?
  • 当然@dexter,我刚刚从管理员更新并添加了登录控制器

标签: laravel laravel-5


【解决方案1】:

尝试按照以下教程进行操作,它对我有用。

附带说明: 避免修改 laravel 附带的现有控制器,而是扩展它并实现自己的功能。这种做法会让您走得更远。

https://www.codementor.io/okoroaforchukwuemeka/9-tips-to-set-up-multiple-authentication-in-laravel-ak3gtwjvt

【讨论】:

  • 仍然出现同样的情况,您是否尝试在未经身份验证的情况下访问管理员私有路由?什么例外给了你?
  • 是的,我创建了一个扩展 LoginController 的 AdminLoginController,并在 AdminLoginController 中覆盖了 Login 方法
猜你喜欢
  • 2013-03-28
  • 2020-04-01
  • 2022-12-10
  • 1970-01-01
  • 2011-09-17
  • 2018-09-26
  • 2010-12-17
  • 1970-01-01
  • 2016-07-09
相关资源
最近更新 更多