Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect using Vue Router in Nuxt JS vuex

I'm building a login/register system as part of an app. I'm using Firebase and Vuex to handle authentication and database information. I have some actions in Nuxt JS that create the user, sign them in and log them out.

I'm trying to implement a redirect after the user has been successfully registered / when they click login, my code is:

export const actions = {

  /*
   * Create a user
   */
  createUser ({commit}, payload) {
    this.$fireAuth.createUserWithEmailAndPassword(payload.email, payload.password).then(function(firebaseUser) {
      commit('setUser', payload)
    }).catch(function(error) {
      console.log('error logging in' + error)
    });
  },

  /*
   * Log a user into Beacon
   */
  login ({commit}, payload) {
    this.$fireAuth.signInWithEmailAndPassword(payload.email, payload.password).then(function(firebaseUser) {
      commit('setUser', payload)
    }).catch(function(error) {
      console.log('error logging in' + error)
    });
  },

  /*
   * Sign a user out of Beacon
   */
  signOut ({commit}) {
    this.$fireAuth.signOut().then(() => {
      commit('setUser', null)
    }).catch(err => console.log(error))
  }

}

I'm using Nuxt JS 2.9.2, on Vue JS 2.6.10

I have a few modules.

I've tried using this.router.push('/') and window.location.href, but would like to retain the SPA functionality.

like image 667
Ryan H Avatar asked Sep 09 '19 16:09

Ryan H


People also ask

Can I use Vue-router in Nuxt?

Nuxt automatically generates the vue-router configuration for you, based on your provided Vue files inside the pages directory. That means you never have to write a router config again!

What is redirect in VUE-router?

A redirect means when the user visits /home , the URL will be replaced by / , and then matched as / . But what is an alias? An alias of / as /home means when the user visits /home , the URL remains /home , but it will be matched as if the user is visiting / .

How do I redirect a page to another page in Vue?

You can use $router. push({ name: "yourroutename"}) or just router. push("yourroutename") now to redirect.


1 Answers

You have two ways to do it:

  1. $nuxt.$router.push in actions
  2. Pass this.$router.push as argument in your action, and call where you need.

Hope this helps.

like image 144
Vladislav Gritsenko Avatar answered Oct 24 '22 15:10

Vladislav Gritsenko