Dassin Rock Mouelet
Dassin Rock Mouelet

Reputation: 71

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(){

  }
}

Upvotes: 4

Views: 5961

Answers (3)

ImBatman
ImBatman

Reputation: 366

This Solution works in Angular 11 without any compilation errors/hacks.

Get the user object from the result after sign-up/sign-in using email authentication & then use it to send a verification email

signUp(email: string, password: string) {
this.afAuth.createUserWithEmailAndPassword(email, password)
  .then((result) => {

    /** sends verification email **/
    result.user.sendEmailVerification();

  }).catch((error) => {
    window.alert(error.message)
  });

}

Upvotes: 6

Carlos Mori
Carlos Mori

Reputation: 408

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');
    });
}

Upvotes: 8

Dassin Rock Mouelet
Dassin Rock Mouelet

Reputation: 71

I found. Just use this function next you have logged user. don't use it if no user is logged.

firebase.auth().currentUser.sendEmailVerification()

Upvotes: 2

Related Questions