【发布时间】:2022-01-18 04:04:11
【问题描述】:
我为 heroku 和 next.js 应用部署了一个 express 服务器。 cookie 是从服务器发送的,甚至显示在网络选项卡中:
但是,cookies 并没有实际存储,我所有的请求都失败了,因为它们依赖于 csrf cookie,并且存储选项卡是空的:
这是在后台设置cookies的代码:
const csrfProtection = csrf({
cookie: {
httpOnly: true,
sameSite: 'none',
secure: true,
},
});
app.set('trust proxy', 1);
app.use(cors({ credentials: true, origin: clientOrigin }));
app.get('/', csrfProtection, function (req: Request, res: Response) {
res.cookie('XSRF-TOKEN', req.csrfToken(), { sameSite: 'none', secure: true });
res.end();
});
app.use(csrfProtection);
这是我的 axios 实例:
const baseURL = process.env.baseURL;
const axiosInstance = Axios.create({
baseURL,
withCredentials: true,
});
和请求代码:
useEffect(() => {
dispatch(loadingAction.setToTrue());
const getCsrf = async () => {
await axiosInstance.get('/');
};
getCsrf()
.then(() => {
dispatch(loadingAction.setToFalse());
})
.catch((err) => {
dispatch(loadingAction.setToFalse());
setError(err);
});
}, []);
【问题讨论】:
标签: node.js reactjs heroku cookies next.js