Ryan H
Ryan H

Reputation: 2945

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.

Upvotes: 6

Views: 5984

Answers (1)

Vladislav  Gritsenko
Vladislav Gritsenko

Reputation: 813

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.

Upvotes: 12

Related Questions