Vue-router + firebase

0

I'm trying to make a login system with vue and firebase, but I'm having trouble redirecting the user after authentication:

login: function () {
  firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(
    (user) => {
      this.$router.push({name: 'dashboard'})
    },
    (err) => {
      alert('Error: ' + err.message)
    }
  )
}

With the code this way when calling the login method I get the following error in the console:

uncaught error during route navigation:

followed by an empty red error on the console

I tried to use a self instead of this, because the first problem that came to mind was this, the result was:

login: function () {
  let self = this
  firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(
    (user) => {
      self.$router.push({name: 'dashboard'})
    },
    (err) => {
      alert('Error: ' + err.message)
    }
  )
}

but the error remains the same.

Has anyone ever been through this?

    
asked by anonymous 25.04.2018 / 00:08

1 answer

0

There is a problem with Scope probably trying to do this:

login() {

  firebase.auth().signInWithEmailAndPassword(this.email, this.password).then(
    (user) => {
      this.$router.push({name: 'dashboard'})
    },
    (err) => {
      alert('Error: ' + err.message)
    }
  )
}

If the problem persists, console this this like this:

login() {
   console.log(typeof this.$router !== 'undefined');
}

If false , because you have a scope problem before this login method. So you need to put the whole code here, to better understand your case.

    
25.04.2018 / 14:12