laravel:如何实际登录用户(ajax登录)

时间:2016-03-02 19:21:51

标签: ajax laravel authentication

不能......记录用户......在......愤怒。

基本上,我正在通过常规登录运行用户,并定义了我自己的经过身份验证的方法来返回json对象而不是重定向。目前,我正在返回此功能的结果

Auth::check();

当完成所有操作时,返回true。 问题是我的登录似乎不存在于此单个调用之外。如果我再进行ajax调用,Auth::check()每次都会失败。如果我刷新页面,auth::check()将失败。

请上帝帮帮我。

PS。我的会话驱动程序当前设置为cookie。

//编辑显示我不会疯狂的人

 public function ajaxLogin(Request $request)
{
    if(Auth::check()){
        return response()
        ->json('loggedIn');
    }

    // 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.
    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

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

    $credentials = $this->getCredentials($request);

    if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
        return $this->handleUserWasAuthenticated($request, $throttles);
    }

    // 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.
    if ($throttles && ! $lockedOut) {
        $this->incrementLoginAttempts($request);
    }


    return response()
    ->json([
            $this->loginUsername() => $this->getFailedLoginMessage(),
        ]);
}
    protected function authenticated($request, $user){
    Auth::login($user);
    return response()->json( 'yo');
}

如果我发送了正确的凭据,我会回来哟。如果我再次发送正确的凭据(另一个ajax调用),我会哟。我永远无法登录。

据我所知,出于某种原因,我的会话正在被每次请求销毁?我不知道为什么,但这似乎是我从未登录的原因。

//编辑添加情况清晰度

laravel -v 5.2

这完全是通过auth控制器,所以只是默认值:

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/home', 'HomeController@index');
});

0 个答案:

没有答案