Php Laravel通过ajax登录维护会话
如何通过Ajax登录维护或创建会话。我有一个Laravel安装,与基本的make:auth安装没有太大区别 我必须编写authcontroller的某些部分来返回json,而不是重定向。以下是登录部分:Php Laravel通过ajax登录维护会话,php,ajax,laravel,authentication,vue.js,Php,Ajax,Laravel,Authentication,Vue.js,如何通过Ajax登录维护或创建会话。我有一个Laravel安装,与基本的make:auth安装没有太大区别 我必须编写authcontroller的某些部分来返回json,而不是重定向。以下是登录部分: /** * Handle an authentication attempt. * * @return Response */ public function login(Request $request) { $this->validate($request, [
/**
* 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());
}
}
以下是使用的路线:
Route::group(['namespace' => 'Auth'], function() {
Route::post('/login', ['uses' => 'AuthController@login', 'as' => 'login']);
Route::post('/registreer', ['uses' => 'AuthController@postRegister', 'as' => 'register']);
Route::post('/reset', ['uses' => 'PasswordController@sendResetLinkEmail', 'as' => 'reset']);
});
我在前端使用Vue.js,它可以完美地获得错误和成功响应。只有在刷新浏览器后,我才没有处于登录状态。我如何做到这一点。使用AJAX/JSON REST API时没有会话。RESTAPI不使用会话,而是使用令牌/某种身份验证 如果要构建REST API并使用VueJS作为单页应用程序的前端框架,请使用
API
中间件,而不是默认的web
中间件
在此处阅读有关JSON Web令牌的更多信息:
在使用Ajax时,您基本上是在创建一个无状态应用程序。前端基本上不需要知道用户的状态,不管他是否已经登录。所以你不需要任何治疗
您需要做的是从服务器获取信息,无论您的用户是否有权获取服务器上的任何资源。这基本上是身份验证(验证发送到服务器的用户凭据,然后将某种id返回给用户的过程)和授权检查id是否被授权访问请求的资源的过程。我想我声明的问题不正确,因为误解。然而,我确实让它发挥了作用。解决方法是在特定路由周围放置中间件。现在我要通过Ajax请求登录
Route::group(['middleware' => 'web'], function () {
...
});
您正在构建REST Api吗?您应该考虑使用令牌而不是设置会话,看看JWT Auth否,我没有构建REST Api。我想用ajax创建一个登录表单,它增加了一个很好的ui感觉。比如airbnb的模式登录,这是一个棘手的问题。看,愿这对你有更好的帮助。