Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

424
Vistas
axios interceptor - hold off the request until cookie API call is finished, and only then continue

I am trying to get the axios to wait until one extra call in the interceptor finishes. So I am using NuxtJS as a frontend SPA and API in Laravel 8.

I've tried a lot of different things over the course of last ~ 4 days but nothing seems to be working.

THE GOAL

I need my axios REQUEST interceptor to check for existence of the cookie. If cookie is not present I need to make an API call first to grab the cookie and then we can continue with any other request.

WHAT I AM DOING?

So basically I have Axios interceptor for the requests that will call cookie endpoint if the cookie doesn't exist.

I am also saving cookie request promise to be reused in case there are multiple calls and the cookie still is not there.

PROBLEM

While it was supposed to just call cookie API first and everything else after I am mostly getting two results in different variations of the attached code.

A) I am making an extra cookie call but it is not in the required order so I still end up hitting laravel endpoint multiple times without cookies which causes extra sessions to spawn.

B) It is not making any calls at all (attached example).

Does anyone know what in the world I am confusing here?

export default function ({$axios, redirect, $cookiz, store}) {
  $axios.onRequest(async request => {
    // make sure that XSRF cookie exists before we make aby calls to prevent backend from
    // creating multiple session when page on load calls more than one endpoint, if we don't have
    // that cookie we will first have to get it and then call the rest of the endpoints
    const xsrfCookie = $cookiz.get('XSRF-TOKEN')
    if (xsrfCookie === undefined || xsrfCookie === null || xsrfCookie === '') {
      await store.dispatch('login/getXsrfCookie')
      $axios.request(request)
    }
    $axios.request(request)
  })
}

  getXsrfCookie(context) {
    if (context.state.xsrfCookiePromise instanceof Promise) {
      return context.state.xsrfCookiePromise
    }
    const xsrfCookiePromise = this.$axios.get('/csrf-cookie').then(response => {
      context.commit('setXsrfCookiePromise', null)
      console.log('This is the cookie response', response)
    })
    context.commit('setXsrfCookiePromise', xsrfCookiePromise)

    return context.state.xsrfCookiePromise
  }

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

I don't know anything about nuxt, and have only a vague idea about axios interceptors, but just looking at the code...

  • I think you want to persist a cookie, not the promise for a cookie.
  • I don't think you need to involve the store.
  • I think you can do that with your cookie plugin. If I'm right about that, using the set method is what you need. (you might need an options param, described here)
async getXsrfCookie() {
  if (!$cookiz.get('XSRF-TOKEN')) {
    // the op should double check which part of the response to persist, whether to stringify it, etc.
    const response = await this.$axios.get('/csrf-cookie');
    $cookiz.set('XSRF-TOKEN', response.data);
  }
}
      
export default function ({$axios, redirect, $cookiz, store}) {
  $axios.onRequest(async request => {
    await getXsrfCookie();
    return $axios.request(request)
  })
}
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda