【发布时间】:2019-03-07 06:32:51
【问题描述】:
我目前正在做登录页面。在将令牌设置为本地存储之前,我对如何检查我登录的用户是有效还是无效有疑问。
我的控制台出错:
POST http://localhost:8000/api/auth/login 401(未授权)
未捕获(承诺中)错误:请求失败,状态码为 401
我的目标: 如果用户有效,则重定向到其他页面,如果无效,则进入的用户将保留在登录页面上。
我使用 Axios.post 发出请求
我这里有 handleSubmit 按钮:
handleSubmit(e) {
e.preventDefault();
axios.post('/api/auth/login',this.state).then(response => {
const token_id = response.data.access_token;
const responsed = response.data;
// localStorage.setItem('token', token_id);
console.log(responsed);
});
}
这是我的函数,它是 JWT 文档中的默认函数:
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if ($token = $this->guard()->attempt($credentials)) {
return $this->respondWithToken($token);
}
return response()->json(['error' => 'Unauthorized'], 401);
}
【问题讨论】:
-
你在用vue路由器吗?
-
我已经解决了这个问题 :) 谢谢罗斯。
-
好的,罗斯,谢谢,我会添加答案