Laravel 5.3 Auth:尝试无法正常工作 [英] Laravel 5.3 Auth:attempt is not working

查看:75
本文介绍了Laravel 5.3 Auth:尝试无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论插入有效或无效的凭据都无关紧要,用户始终会重定向到RedirectIfAuthenticated中间件中定义的路由.我的问题:如何将经过身份验证的用户重定向到与未经身份验证的用户不同的路由?

Dont matter if i insert valid or invalid credentials, the user is always redirect to the route defined in RedirectIfAuthenticated middleware. My question: How can i redirect authenticated users to different route than non authenticated user?

RedirectIfAuthenticated(中间件):

RedirectIfAuthenticated (Middleware):

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/');
        }

        return $next($request);

这是我的LoginController:

This is my LoginController:

   public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

public function authenticate(Request $data){
    if (Auth::attempt( ['cnpj' => $data['cnpj'], 'password' => $data['password'] ] )){
        return redirect()->route('someroute');
    }
    else{
        return redirect()->route('login');

    }

路线:

Route::get('/', function () {
    return view('welcome');
}) -> name('/');

Route::post('/register', 'Auth\RegisterController@create');

Route::get('/register', function () {
    return view('auth.register');
});

Route::get('/login', function (){
    return view('auth.login');
})->name('login');

Route::post('/login', 'Auth\LoginController@authenticate');

推荐答案

在您的LoginController中,您已将guest中间件设置为适用于除logout以外的所有路由.这意味着经过身份验证的用户永远无法进入authenticate方法,因为中间件已经运行并且已经重定向了该用户.

In your LoginController, you have set the guest middleware to apply to all routes except for logout. That means that a user who is authenticated can never get to the authenticate method because the middleware has already been run and has redirected the user.

最简单的解决方法是将authenticate方法添加到guest中间件的排除项中:

The simplest fix would be to add the authenticate method to the exclusions for the guest middleware:

public function __construct()
{
    $this->middleware('guest', ['except' => ['logout', 'authenticate']]);
}


从您的问题来看,您似乎并不真正了解Laravel的中间件,路由和身份验证的概念.我强烈建议您重新阅读有关这些功能的文档,因为您似乎正在尝试手动实现框架中已经存在的功能.


It appears from your question that you don't really understand Laravel's concepts of Middleware, Routing, and Authentication. I highly recommend that you reread the documentation on those features, because it looks like you are trying to manually implement features that already exist in the framework.

这篇关于Laravel 5.3 Auth:尝试无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆