Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email Verification with Firebase using AngularFire v6

I am trying to register User with email and password. If register function works then a mail will be send to user's email address, this mail must contain a link that the user will click to confirm email address. I have try to do this with the AngularFireAuth.auth.currentUser.sendEmailVerification() function it doesn't work: This version of AngularFire/Firebase does'nt support this method. Can you help me ?

import {Injectable, NgZone} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {first} from 'rxjs/internal/operators/first';
import * as firebase from 'firebase';
import {AngularFirestore} from '@angular/fire/firestore';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  public userId: string;

  constructor(
      private afAuth: AngularFireAuth,
      private afStore: AngularFirestore,
      public ngZone: NgZone
  ) { }

  getUser(): Promise<firebase.User>{
    return this.afAuth.authState.pipe(first()).toPromise();
  }
  loginWithEmailAndPassword(email: string, password: string): Promise<firebase.auth.UserCredential>{
    return this.afAuth.signInWithEmailAndPassword(email, password);
  }
  async registerWithEmailAndPassword(email: string, password: string): Promise<firebase.auth.UserCredential>{
    const newUserCredential: firebase.auth.UserCredential = await this.afAuth.createUserWithEmailAndPassword(email, password);
    this.afStore.collection('users').doc(`${newUserCredential.user.uid}`).set({
      uid: newUserCredential.user.uid,
      email: newUserCredential.user.email,
      created_at: firebase.firestore.FieldValue.serverTimestamp()
    }).then(function() {
      console.log("user is registered");
      this.sendEmailVerification();
    }).catch(function(error) {
      console.error("Error adding document: ", error);
    });
    return newUserCredential;
  }

  async sendEmailVerification() {
    
  }

  resetPassword(email: string): Promise<void>{
    return this.afAuth.sendPasswordResetEmail(email);
  }

  logout(): Promise<void>{
    return this.afAuth.signOut();
  }

  async createPersonalProfile(){

  }
}
like image 579
Dassin Rock Mouelet Avatar asked Oct 18 '25 09:10

Dassin Rock Mouelet


1 Answers

The new API returns an observable when you want to access currentUser. So I did this:

async SendVerificationMail() {
    (await this.afAuth.currentUser).sendEmailVerification().then(() => {
        console.log('email sent');
    });
}
like image 132
Carlos Mori Avatar answered Oct 21 '25 14:10

Carlos Mori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!