Laravel通过ajax登录维护会话

时间:2016-07-02 16:20:09

标签: php ajax laravel authentication vue.js

我如何通过Ajax登录维护或创建会话。我有一个Laravel安装与基本的make:auth安装没那么太不同。

我不得不过度编写authcontroller的某些部分来返回json而不是重定向。这是登录部分:

+ab

以下是使用的路线:

/**
 * Handle an authentication attempt.
 *
 * @return Response
 */
public function login(Request $request)
{
    $this->validate($request, [
        $this->loginUsername() => 'required',
        'password' => 'required'
    ]);

    // 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 && $this->hasTooManyLoginAttempts($request)) {
        // to many attempts
        if ( $request->ajax() )
        {
            $this->response['code'] = 406;
            $this->response['message'] = $this->sendLockoutResponse($request);

            return response()->json($this->response);
        }

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

    $credentials = $this->getCredentials($request);
    $credentials['is_active'] = 1;

    if (Auth::attempt($credentials, $request->has('remember'))) {
        // succes
        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) {
        $this->incrementLoginAttempts($request);
    }

    // error
    if ( $request->ajax() )
    {
        $this->response['code'] = 406;
        $this->response['message'] = $this->getFailedLoginMessage();

        return response()->json($this->response);
    }

    return redirect()->back()
        ->withInput($request->only($this->loginUsername(), 'remember'))
        ->withErrors([
            $this->loginUsername() => $this->getFailedLoginMessage()
        ]);
}

/**
 * Handle an authenticated response.
 *
 * @return Response
 */
public function authenticated($request, $user)
{
    if ( $request->ajax() )
    {
        $this->response['message'] = "success";

        return response()->json($this->response);
    }
    else
    {
        return redirect()->intended($this->redirectPath());
    }
}

我正在使用前端的Vue.js,它可以完美地获得错误和成功响应。只有在刷新浏览器后,我才处于登录状态。我怎么能做到这一点。

3 个答案:

答案 0 :(得分:1)

使用AJAX / JSON REST API时没有会话。 REST API使用令牌/某种身份验证而不是会话。

如果要构建REST API并将VueJS用作单页面应用程序的前端框架,请使用api中间件而不是默认的web中间件。

在此处阅读有关JSON Web令牌的更多信息: https://jwt.io/introduction/

答案 1 :(得分:0)

在使用Ajax时,您基本上是在创建无状态应用程序。前端方面基本上不需要知道用户的状态,他是否已经登录。所以你不需要任何会话。

您需要做的是从服务器获取信息,您的用户有权获取服务器上的任何资源。这基本上是Authenticating(验证发送到服务器然后向用户返回id的用户凭证的过程)和授权过程以检查id是否有权访问所请求的资源。

答案 2 :(得分:0)

由于误解,我猜我的问题不正确。但是我确实让它发挥作用。解决方法是将中间件放在特定路径周围。现在我要通过Ajax请求登录。

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