Laravel Ajax登录尝试重定向

时间:2016-11-26 15:50:55

标签: ajax laravel-5 laravel-5.3

我尝试在经过身份验证后删除重定向的经过身份验证的用户的默认Laravel行为。

我在LoginController.php

中发表了评论
//protected $redirectTo = '/home'; 

并且还更改了AuthenticatesUsers供应商文件以返回JSON而不是登录成功时的重定向:

protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        /*return $this->authenticated($request, $this->guard()->user())
                ?: redirect()->intended($this->redirectPath());*/

                 return response()->json(['SUCCESS' => 'AUTHENTICATED'], 200);
    }

但是当我成功登录时,我在Chrome网络标签中看到我收到错误,因为它试图将我重定向到/home这是一个未定义的路由,导致错误并停止我的Laravel SPA

为什么即使在我发表评论并更改供应商文件后,Laravel仍继续指示我/home

1 个答案:

答案 0 :(得分:4)

这是一个中间件问题。

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

        return $next($request);
    }

在,app/Http/Middleware/RedirectIfAuthenticated.php