身份验证中间件返回像响应,而不是错误

时间:2019-04-24 01:47:08

标签: reactjs authentication redux middleware

在节点上我有此代码

const auth = (req, res, next) => {
const token = req.header('x-auth-token');

//Check Token
if(!token) return res.status(400).json({msg: "No Token, authorizaton denied"});

//Verify Token
try {
    const decoded = jwt.verify(token, keys.JWTSecret);
    //Add user from payload
    req.user = decoded;
    next();
} catch(e) {
    res.status(400).json({error: 'Token is not valid'})
}

}

关于Redux的操作,我这样:

xport const loadUser = () => (dispatch, getState) => {
dispatch({type: USER_LOADING});

const token = getState().auth.token;

const config = {
    headers: {
        "Content-type": "application/json"
    }
}

if (token) {
    config.headers['x-auth-token'] = token;
}


fetch('/api/profile', config)
    .then(response => response.json())
    .then(user => dispatch({
        type: USER_LOADED,
        payload: user
    }))
    .catch(err => {
        dispatch(returnErrors(err.response.data, err.response.status))
        dispatch({
            type: USER_FAIL
        })
    })
}

但是当令牌不存在并且验证不通过时,我得到“没有令牌,验证被拒绝”,我在响应中得到这个,以此类推,在USER_LOADED上,而不在USER_FAIL上,为什么?为什么?

0 个答案:

没有答案