1
votes

I'm trying to send an email verification to the user when they sign up. The user gets added to firebase but the email is not sent. I logged the error to the console and it says

Cannot read property 'sendEmailVerification' of undefined

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { User } from "../../models/user";
import { AngularFireAuth } from "angularfire2/auth";
import * as firebase from 'firebase';


@IonicPage()
@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage {

  user = {} as User

  constructor(private afAuth: AngularFireAuth,
    public navCtrl: NavController, public navParams: NavParams) {
  }

  async register(user: User) {
     try {
       const result = await this.afAuth.auth.createUserWithEmailAndPassword(user.email, user.password)
       .then(res => {
         var auth = firebase.auth();
         this.auth.sendEmailVerification(user.email)
         this.navCtrl.setRoot('LoginPage');
       }
     }
     catch (e) {
       console.log(e);
     }
   }

}
2

2 Answers

1
votes

First thing you cannot use this to reference the variable as it is declared locally inside the same function.

Try removing the this keyword or optionally you can achieve email verification as follows:

inside your then function after registering:

 let user = firebase.auth().currentUser;
 user.sendEmailVerification();
 this.navCtrl.setRoot('LoginPage');
0
votes

It looks like you are using this.auth.sendEmailVerification but you are never actually assigning this.auth.

Try changing this.auth.sendEmailVerification to either auth.sendEmailVerification since you have that variable available from the line before or to this.afAuth.auth.sendEmailVerification.