how to send fetch request with ssr nuxt and httpOnly auth

3k views Asked by At

I have an SSR nuxt app and laravel backend. In auth, we are using HttpOnly and we are Ok in most of the code but there is a problem in sending requests with fetch and asyncData on the server-side.

I know that only the browser automatically merge a header for auth with request and send it. Now I'm using fetch and this will send requests to the server and there is no browser on the server.

I searched for making custom fetch in nuxt, but I don't find any. How should I send a request with fetch and asyncdata and do not get a 401 error?


UPDATE

***: I have to change nuxt mode to spa from ssr unfortunately, because I don't find any answer yet.

2

There are 2 answers

2
er8h On

fetch won’t send cookies, unless you set the credentials init option. Try setting the credentials option to same-site or include like this:

fetch('your-endpoint', { credentials: 'same-site' }).then(...)
0
Esamani77 On

I still haven't found an answer. I changed axios part in nuxt.config.js like this:

axios: {
    baseURL: 'https://api.iranicard.net/api/v1/admin/',
    credentials: 'same-origin',
    common: {
      'Accept': 'application/json',
      "Content-Type": "application/json"
    },
    retry: { retries: 2 },
    proxyHeaders: false
  },

But still after a successful login, when in a component that I used fetch or asyncData hook, I got logout and it's not working.