req.headers [' x-access-token']为空

时间:2018-06-07 11:36:41

标签: javascript node.js express axios

我正在尝试使用axios通过发送x-access-token标头来使用令牌身份验证,并在我的服务器中检查时将null作为标头值。

FRONT:

  fetchUser = () => {

    let data = {
      username: this.state.username
    }
    axios({
      method: 'post',
      url:'https://server.com/getuser',
      data: { 
        username: this.state.username
      },
      headers: {
        'Access-Control-Allow-Headers': 'x-access-token',
        'x-access-token': this.state.apiToken
      }
      })
      .then((res) => {
        console.log(res.data.user)
      })
      .catch(() => {
        console.log('was problem adding credit in server')
      })
  } 

服务器:

router.post('/', (req, res) => {

    var token = req.headers['x-access-token'];
    console.log('this is req.headers', token)
    if (!token) return res.status(401).send({ auth: false, message: 'No token provided.' });

    jwt.verify(token, config.secret, function(err, decoded) {
      if (err) {
        console.log('error with token')
        conaole.log('decoded token',decoded)
        return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
        }
        UserData.findOneAndUpdate(
            {username: req.body.username}
            (err, user) => { 
                if(err) {
                    console.log('error ,' , err) 
                    res.send(err);
                } else {

                    res.send(user);

                }
            }
        );
    })  
})

0 个答案:

没有答案