我在获取发送到前端的jwt时遇到问题。
async login(req,res){
const loginExist = await User.existLogin(req.body.login);
console.log(loginExist)
if (!loginExist) { return res.status(400).json({result: 'Login is wrong '});}
const isPassword = await User.isPassword(loginExist.dataValues.password, req.body.password);
console.log(isPassword)
if (!isPassword) { return res.status(400).json({result});}
console.log('sem error')
console.log(config.secretToken)
const token = await jwt.sign({id: loginExist.dataValues.id}, config.secretToken);
const results = {
token,
loginExist
}
console.log(token);
res.status(200);
res.send(results);
}
如果我对登录名和密码进行身份验证,我会将jwt和用户数据发送到我的前端
signIn = () => {
const requestInfo = {
method:'POST',
body: JSON.stringify({login:this.login,password:this.password}),
headers: new Headers({
'Content-Type': 'application/json'
}),
};
fetch('http://localhost:8080/login', requestInfo)
.then(response => {
if(response.ok){
return JSON.stringify(response.json());
}
throw new Error("Login Invalido..")
})
.then(token => {
console.log(token.token);
sessionStorage.setItem('token', JSON.stringify(token.token));
//this.props.history.push("/users");
return;
})
.catch(e => {
this.setState({message: e.message})
});
}
在此方法中,我无法获得令牌和用户
我也无法发送状态响应和包含我的令牌和用户的json
res.status(200).json({token}, {loginExist});
express deprecated res.json(status, obj): Use res.status(status).json(obj) instead src\controllers\UserController.js:42:25
(node:11140) UnhandledPromiseRejectionWarning: RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: {
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwiaWF0IjoxNTczNzY0NzMxfQ.YA_8x6nY4qIuDh0Ehcsg4jtC37y1LcmFKYFonkoVECM'
}
at ServerResponse.writeHead (_http_server.js:241:11)
at ServerResponse._implicitHeader (_http_server.js:232:8)
at write_ (_http_outgoing.js:607:9)
at ServerResponse.end (_http_outgoing.js:717:5)
at ServerResponse.send (C:\Users\SpiriT\Documents\ApiGame\node_modules\express\lib\response.js:221:10)
at ServerResponse.json (C:\Users\SpiriT\Documents\ApiGame\node_modules\express\lib\response.js:267:15)
at login (C:\Users\SpiriT\Documents\ApiGame\src\controllers\UserController.js:42:25)
(node:11140) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11140) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.