Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
NuxtJS Auth redirects before second API call

when I run the following method on my login page, I get some weird behaviour. After the login() method is called, I want to then run a separate call to fetch profile details & permissions, then redirect to /home, but instead it redirects straight to /, before the second api call has been returned:

attempt() {
      try {
        let data = {
          email: this.creds.email,
          password: this.creds.password
        }

        this.$auth.login({data: data}).then(() => {
          //I want this API call to run BEFORE redirecting
          this.axios.get(this.makeAuthUrl('user/'+process.env.NUXT_ENV_COMMUNITY_ID), this.getHeaders(false)).then(user => {
            console.log(user.data)
            this.$store.commit("SET_PROFILE", user.data.profile)
            this.$store.commit("SET_ABILITIES", user.data.abilities)
            this.actionLoading = false;
            this.$router.push({path: "/home"})
          })
        })

      } catch (e) {
        console.log(e)
        this.actionLoading = false;
        this.$toast.error("Invalid Credentials!");
      }
    }

So how can I modify the code in order to delay the redirect until I explicitly redirect the user?

Using nuxtjs/auth module

Thanks! EDIT - Added async await for explanation

async attempt() {
      try {
        let response = await this.$auth.loginWith('local', {
          data: {
              email: this.creds.email,
              password: this.creds.password
            }
        })
        this.axios.get(this.makeAuthUrl('user/'+process.env.NUXT_ENV_COMMUNITY_ID), this.getHeaders(false)).then(user => {
          console.log(user)
          this.$store.commit("SET_PROFILE", user.data.profile)
          this.$store.commit("SET_ABILITIES", user.data.abilities)
          this.actionLoading = false;
          this.$router.push({path: "/"});
        })
      } catch (e) {
        console.log(e)
        this.actionLoading = false;
        this.$toast.error("Invalid Credentials!");
      }
    }

Edit 2

try {
        let response = await this.$auth.loginWith('local', {
          data: {
            email: this.creds.email,
            password: this.creds.password
          }
        })
        let response2 = await this.axios.get(this.makeAuthUrl('user/'+process.env.NUXT_ENV_COMMUNITY_ID), this.getHeaders(false))
        if(response2.status === 200) {
          console.log(response2.data)
          this.$store.commit("SET_PROFILE", response2.data.profile)
          this.$store.commit("SET_ABILITIES", response2.data.abilities)
          await this.$router.push({path: "/"});
        }
      } catch (e) {
        console.log(e)
        this.actionLoading = false;
        this.$toast.error("Invalid Credentials!");
      }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Fixed with the following:

async attempt() {
      try {
        let data = {
          email: this.creds.email,
          password: this.creds.password
        }
        let response = await this.axios.post(this.makeAuthUrl('login'), data, this.getHeaders(false))
        if(response.status === 200) {
          this.$auth.strategy.token.set(response.data.token)
        }
        let getUser = await this.axios.get(this.makeAuthUrl('user/'+process.env.NUXT_ENV_COMMUNITY_ID), this.getHeaders(false))
        if(getUser.status === 200) {
          this.$auth.setUser(getUser.data.user)
          this.$store.commit("SET_PROFILE", getUser.data.profile)
          this.$store.commit("SET_ABILITIES", getUser.data.abilities)
          return this.$router.push({path: "/"});
        }
      } catch (e) {
        this.actionLoading = false;
        this.$toast.error("Invalid Credentials!");
      }
    }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda