Reactjs 在我的头我有令牌,但它说没有令牌,好像没有令牌。但当它在邮递员身上试用时,效果很好。这里有什么问题
这是我的行动代码:Reactjs 在我的头我有令牌,但它说没有令牌,好像没有令牌。但当它在邮递员身上试用时,效果很好。这里有什么问题,reactjs,express,redux,react-redux,mern,Reactjs,Express,Redux,React Redux,Mern,这是我的行动代码: export const accept = (clinicianId, duration) => (dispatch, getState) => { axios .post( "http://localhost:5000/api/patient/authAccess", clinicianId, duration, tokenConfig(getState) ) .then(res
export const accept = (clinicianId, duration) => (dispatch, getState) => {
axios
.post(
"http://localhost:5000/api/patient/authAccess",
clinicianId,
duration,
tokenConfig(getState)
)
.then(res =>
dispatch({
type: ACCESS,
payload: res.data
})
)
.catch(err => {
dispatch(
returnErrors(err.response.data, err.response.status, "ACCESS_FAIL")
);
});
};
这是我设置令牌并将其添加到标头的方式:
export const tokenConfig = getState => {
const token = getState().auth.token;
const config = {
headers: {
"Content-type": "application/json"
}
};
if (token) {
config.headers["x-auth-token"] = token;
}
return config;
};
这是我的api,令牌将转到auth函数并授权用户:
const { clinicianId, duration } = req.body;
jwt.sign(
{ id: clinicianId },
config.get("jwtSecretAccess"),
{ expiresIn: duration },
(err, token) => {
if (err) throw err;
res.json({
token
});
}
);
当我检查令牌的值时,有一个我认为api不能读取它?我不知道现在该怎么办。您可以使用Axios拦截器,因为您可以为每个http请求添加头