Vue-router + firebase

Asked

Viewed 119 times

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.

someone has been there?

  • as to the scope, the arrow function does not create a new scope, so your first example is correct. then I believe your problem is related to the route itself, so if possible share your routes.

1 answer

0

There is a scopo problem probably, try to do this way:

login() {

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

If the problem pesistir, console this this in this way:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.