自定义Guard身份验证异常

时间:2019-04-29 11:17:53

标签: laravel laravel-5

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

我的仪表板控制器自定义防护是:

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

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

添加我的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');
    }

}

1 个答案:

答案 0 :(得分:0)

尝试遵循以下教程,它对我有用。

附带说明:避免修改laravel随附的现有控制器,而是对其进行扩展并实现您自己的功能。这种做法将带您走很长一段路。

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