Remix会话值不会跨页面保持。

huangapple go评论94阅读模式
英文:

Remix session values don't persist across pages

问题

我正在使用Remix创建一个网站,我想要在不同页面之间持久化会话值。我认为问题出在getSession请求上,因为这些值在对同一页面的多次请求之间不会持久化。我在sessions.ts中实现了一个会话Cookie:

  1. const { getSession, commitSession, destroySession } =
  2. createCookieSessionStorage<SessionData, SessionFlashData>(
  3. {
  4. // 创建Cookie的选项
  5. cookie: {
  6. name: "__session",
  7. maxAge: 1200,
  8. path: "/",
  9. sameSite: "none",
  10. secure: true,
  11. secrets: ["surprise"]
  12. },
  13. }
  14. );

在一个页面上,我设置了一个值并将其记录下来,并获得了预期的值:

  1. export const loader = async ({ request }: LoaderArgs) => {
  2. const session = await getSession(
  3. request.headers.get("Cookie")
  4. );
  5. session.set("token", "abc123")
  6. var data = { "count": 2 }
  7. console.log(session.get("token"))
  8. return json(data, {
  9. headers: {
  10. "Set-Cookie": await commitSession(session),
  11. },
  12. });
  13. };

然而,当我尝试在另一个页面访问该值时,该值为undefined:

  1. export const loader = async ({ request }: LoaderArgs) => {
  2. const session = await getSession(
  3. request.headers.get("Cookie")
  4. );
  5. var data = { "abc": 442 }
  6. console.log(session.get("token"))
  7. return json(data, {
  8. headers: {
  9. "Set-Cookie": await commitSession(session),
  10. },
  11. });
  12. return null
  13. };

我对Remix和React非常新,请帮助我解决问题,谢谢!

英文:

I'm making a site using Remix where I'd like to persist session values across pages. I think the issue lies in the getSession request, as the values do not persist across requests to the same page. I have implemented a session cookie in sessions.ts:

  1. const { getSession, commitSession, destroySession } =
  2. createCookieSessionStorage&lt;SessionData, SessionFlashData&gt;(
  3. {
  4. //cookie options to create a cookie
  5. cookie: {
  6. name: &quot;__session&quot;,
  7. maxAge: 1200,
  8. path: &quot;/&quot;,
  9. sameSite: &quot;none&quot;,
  10. secure: true,
  11. secrets: [&quot;surprise&quot;]
  12. },
  13. }
  14. );

On one page I set a value and log it out and receive the expected value

  1. export const loader = async ({ request }: LoaderArgs) =&gt; {
  2. const session = await getSession(
  3. request.headers.get(&quot;Cookie&quot;)
  4. );
  5. session.set(&quot;token&quot;, &quot;abc123&quot;)
  6. var data = { &quot;count&quot;: 2 }
  7. console.log(session.get(&quot;token&quot;))
  8. return json(data, {
  9. headers: {
  10. &quot;Set-Cookie&quot;: await commitSession(session),
  11. },
  12. });
  13. };

however when i try to access the value in a different page, the value is undefined

  1. export const loader = async ({ request }: LoaderArgs) =&gt; {
  2. const session = await getSession(
  3. request.headers.get(&quot;Cookie&quot;)
  4. );
  5. var data = { &quot;abc&quot;: 442 }
  6. console.log(session.get(&quot;token&quot;))
  7. return json(data, {
  8. headers: {
  9. &quot;Set-Cookie&quot;: await commitSession(session),
  10. },
  11. });
  12. return null
  13. };

I'm very new to remix and react so appreciate any help!

答案1

得分: 0

eht eussi saw ot od htiw eht emitSemas noitpo dna ecurS snoitpo.
sa I ma gnikrow ylcol, ecruS tsum eb tes ot eslaf hcihw snaem emitSemas tsu tsum eb rehtie xal ro tsirts

英文:

the issue was to do with the sameSite option and Secure options.
as I am working locally, Secure must be set to false which means sameSite must be either lax or strict

答案2

得分: 0

@Ov_.01的回答是正确的。我不得不将我的secureprocess.env.NODE_ENV === &#39;production&#39;更改为明确的false。这是我完整的Cookie,允许会话在本地持续存在。

  1. const sessionStorage = createCookieSessionStorage({
  2. cookie: {
  3. // secure: process.env.NODE_ENV === &#39;production&#39;,
  4. secure: false, // TODO: 在使用process.env.NODE_ENV === &#39;production&#39;时本地会话不会持续存在
  5. secrets: [process.env.SESSION_SECRET],
  6. sameSite: &#39;lax&#39;,
  7. maxAge: 30 * 24 * 60 * 60, // 30天
  8. httpOnly: true
  9. }
  10. })
英文:

Answer from @Ov_.01 is correct. I had to change my secure from process.env.NODE_ENV === &#39;production&#39; to an explicit false. This was my full cookie that allowed sessions to persist for me locally.

  1. const sessionStorage = createCookieSessionStorage({
  2. cookie: {
  3. // secure: process.env.NODE_ENV === &#39;production&#39;,
  4. secure: false, // TODO: local session doesn&#39;t persist when using process.env.NODE_ENV === &#39;production&#39;
  5. secrets: [process.env.SESSION_SECRET],
  6. sameSite: &#39;lax&#39;,
  7. maxAge: 30 * 24 * 60 * 60, // 30 days
  8. httpOnly: true
  9. }
  10. })

huangapple
  • 本文由 发表于 2023年6月2日 03:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定