Git Product home page Git Product logo

Comments (4)

stackblitz avatar stackblitz commented on July 19, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

from nuxt.

danielroe avatar danielroe commented on July 19, 2024

You would need to set the cookie returned by useFetch on the HTML response. This isn't done automatically right now, but we might consider doing it for useFetch.

https://nuxt.com/docs/getting-started/data-fetching#pass-cookies-from-server-side-api-calls-on-ssr-response

from nuxt.

yesworld avatar yesworld commented on July 19, 2024

@danielroe thanks for the answer!

But that doesn't work for me either. I created a composable fetchWithCookie and used it in my code. The sessionId is different every time.

<script setup>
const event = useRequestEvent();

const { data } = await useAsyncData(() => fetchWithCookie(event, '/api/test'));
</script>
<template>
  <div>
    <p>
      If you simply refresh the page, there will be a new session every time.
    </p>
    <pre>{{ data }}</pre>

    <p>
      But if you go straight to endpoint:
      <a href="/api/test">/api/test</a>, the session id will be remain
      permanent. Just what I need!
    </p>
  </div>
</template>

from nuxt.

yesworld avatar yesworld commented on July 19, 2024

I solved my issue via middleware. And it’s strange, it doesn’t work in the endpoint, but works in the middleware. How can it be?

/server/middleware/session.ts

export default defineEventHandler(async (event) => {
  console.log('middleware', {
    sessionId: event.context.sessionsId,
    cookie: getCookie(event, 'seesion-id-test-2'),
  })

  if (event.context.sessionsId) {
    return
  }

  const session = await getSession(event, {
    password: '80d42cfb-1cd2-462c-8f17-e3237d9027e9',
    name: 'seesion-id-test-2',
    cookie: {
      maxAge: 3600,
      path: '/',
      sameSite: true,
    },
  })

  event.context.sessionsId = session.id
})

from nuxt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.