从 json 响应设置时 Cookie 不保存

发布于 2025-01-12 00:35:48 字数 827 浏览 2 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2025-01-19 00:35:48

确保在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文