我目前正在做登录页面。在将令牌设置为本地存储之前,如何检查我登录的用户是有效的还是无效的,我有问题。
控制台上的错误:
发布http://localhost:8000/api/auth/login 401 (未经授权)未授权错误:状态代码401请求失败
My的目标:如果用户有效,则重定向到其他页面,如果无效,则输入的用户将保留在登录页面上。
我使用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);
}发布于 2018-10-02 08:28:51
若要验证登录用户
我只是附加条件。
handleSubmit(e) {
e.preventDefault();
axios.post('/api/auth/login',this.state).then(response => {
const token_id = response.data.access_token;
localStorage.setItem('token', token_id);
var get_token = localStorage.getItem('token');
if(get_token === null)
{
return <Redirect to='/login' />
}
else
{
this.props.history.push('/hiflyer-dashboard');
}
}).catch((error) =>{
if(error.response)
{
alert('Invalid');
}
})
}https://stackoverflow.com/questions/52603130
复制相似问题