[Vuejs]-Firebase create user error is caught after then block is executed

1đź‘Ť

âś…

You should return the promise returned by the asynchronous set() method to correctly chain the promises, and also adapt the last then() block which does not contain any callback function.

So the following should do the trick:

    firebase
      .auth()
      .createUserWithEmailAndPassword(this.email, this.password)
      .then((cred) => {
        return db.collection('admin').doc(cred.user.uid).set({
          admin: this.$route.params.admin,
          email: this.email,
          fname: this.firstName,
          phone: this.phone,
          surname: this.lastName,
          'user.uid': cred.user.uid,
        });
      })
      .then(() => {
        this.$router.push({ name: 'Login' });
      })
      .catch((err) => {
        console.log(err.message);
        this.rePasswordFeedback = err.message;
      });

Note that you may not need to reroute to the login page, since the call to the createUserWithEmailAndPassword() actually logs in the user when successful, as mentioned in the doc (“On successful creation of the user account, this user will also be signed in to your application”). So you could directly reroute it to a “secured” page.

👤Renaud Tarnec

Leave a comment