【发布时间】:2020-03-04 18:04:41
【问题描述】:
这是我的操作代码:
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 无法读取它?我现在不知道该怎么办。
【问题讨论】:
标签: reactjs express redux react-redux mern