Wal*_*eed 7 ajax login laravel laravel-5.4
假设我有一个使用auth中间件的页面A. 由于没有登录,它会被重定向到登录页面.
在登录页面上,我有自定义ajax登录系统.在成功登录时,我想使用相同的URL重定向到页面A,以便可以完成操作.
我的登录代码是这样的:
public function postLogin(Request $request)
{
$auth = false;
$errors = [];
$inputs = $request->all();
$validator = $this->validator($inputs);
if ($validator->fails()) {
return response()->json([
'auth' => false,
'intended' => URL::previous(),
'errors' => $validator->errors()
]);
}
$user = User::where('email', $request->get('email'))->first();
if ($user && $user->is_active == 0) {
$errors[] = "This account has been deactivated";
} else if ($user && $user->confirm_token != null) {
$errors[] = "Please verify your email in order to login";
} else {
$credentials = ['email' => $request->get('email'), 'password' => $request->get('password'), 'is_active' => 1];
if (Auth::attempt($credentials, $request->has('remember'))) {
$auth = true;
} else {
$errors[] = "Email/Password combination not correct";
}
}
if ($request->ajax()) {
return response()->json([
'auth' => $auth,
'intended' => URL::previous(),
'errors' => $errors
]);
}
return redirect()->intended(URL::route('dashboard'));
}
Run Code Online (Sandbox Code Playgroud)
我试图通过url() - > previous()获取以前的url,但它返回登录页面url.请有人指导我.任何改进/帮助将不胜感激.
我正在使用Laravel 5.4
小智 6
我这里有一个非常类似的问题:Laravel 5.6上的Ajax Auth重定向
正如@aimme(/sf/users/98679521/)指出的那样,Ajax调用是无状态的,因此基本上您无法与后端进行交互。
他的建议和我的建议是在URL中传递要重定向的页面,或者在您的情况下,可以通过post参数传递给它,例如:
return response()->json([
'auth' => false,
'intended' => $request->intended,
'errors' => $validator->errors()
]);
Run Code Online (Sandbox Code Playgroud)