【发布时间】:2018-05-16 14:01:39
【问题描述】:
我目前有一个从 Vue 组件登录时点击的身份验证功能。现在它使用户登录,但控制器没有发生重定向。我不确定是否使用 Vue 组件会导致这种情况。如果是这样,也许我可以在响应中返回预期的 URL?
public function authenticate(Request $request)
{
//Validate the login and log errors if any
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
//if they have stuff posted get it
$email = $request->get('email');
$password = $request->get('password');
//See if they are actually a user
if (Auth::attempt(['email' => $email, 'password' => $password])) {
return redirect()->intended('/dashboard');
} else {
return response()->json([
'response' => 'error',
'error' => 'Email or Password not correct.',
]);
}
}
我的 login.vue 组件中的登录方法:
login(){
this.isLoading = true;
this.form.post('/login')
.then(data => {
this.isLoading = false
if(data.response == 'success'){
//Maybe get a url in the response and redirect here??
} else {
this.serverError= data.error
}
})
.catch(error => {
this.isLoading = false
})
}
使用 Laravel 5.4
【问题讨论】:
-
从 vuejs 重定向。不是来自 laravel
-
在我看来,从 Laravel 重定向 RESTful API 并没有多大意义。我会返回用户实例或类似的东西并从前端进行重定向。
-
我刚刚发布了对我有用的答案。我只是将预期的 URL 返回到我的前端并让它处理它。谢谢@user2486
-
不要忘记选择您自己的答案作为已接受。
-
您没有使用开箱即用的身份验证设置的任何原因,默认情况下会为您完成这一切吗?