从 json 响应设置时 Cookie 不保存
我试图在 oAuth 之后保存用户的 access_token 和 refresh_token。
流程:
- 用户进行身份验证,我从社交网站检索他们的 accessToken、refreshToken 和用户数据
- 我将数据发送到混音资源路由
- 在混音资源路由的操作中,我保存数据,然后尝试设置访问令牌和使用“Set-Cookie”刷新令牌,但它不起作用。
包括我尝试设置 cookie 的 /api/setUser 资源路由的相关部分
export let action: ActionFunction = async ({ request }) => {
const session = await sessionStorage.getSession(
request.headers.get('Cookie')
);
const jsonData = await request.json();
session.set('access_token', jsonData['accessToken']);
session.set('refresh_token', jsonData['refreshToken']);
return json<LoaderData>(
{ status: 'ok' },
{
headers: {
'Set-Cookie': await sessionStorage.commitSession(session),
},
}
);
};
I am trying to save the access_token and refresh_token of a user after oAuth.
Flow:
- User authenticates and I retrieve their accessToken, refreshToken, and user data from the social site
- I send data to a remix resource route
- In the action for the remix-resource route, I save the data and then try to set the access token and refresh token using 'Set-Cookie' but it doesn't work.
Including the relevant part of my /api/setUser resource route where I try to set the cookie
export let action: ActionFunction = async ({ request }) => {
const session = await sessionStorage.getSession(
request.headers.get('Cookie')
);
const jsonData = await request.json();
session.set('access_token', jsonData['accessToken']);
session.set('refresh_token', jsonData['refreshToken']);
return json<LoaderData>(
{ status: 'ok' },
{
headers: {
'Set-Cookie': await sessionStorage.commitSession(session),
},
}
);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保在
createCookieSessionStorage
中将 cookie 路径设置为/
,否则,cookie 仅在您设置的路由上可见。Make sure that in your
createCookieSessionStorage
that you have the cookie path set to/
otherwise, the cookie is only visible on the route you set it.