Generate verification mail through Firebase using Angular/Spring Boot

Issue

I’m developing an app with Firebase (authentication only), Angular and Spring Boot. I would like to send an email verification once a user is created. I’ve two options to create a user by giving email and password.

  1. Registering through registration page (anyone)
  2. An admin can create a user after signed-in into the system.

In Angular 7

SignUp(email, password) {
    return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
    .then((result) => {
        // You have been successfully registered!"  
        this.afAuth.auth.currentUser.sendEmailVerification()
            .then(() => {
                // Please verify your email
            })
    }).catch((error) => {
        // Error while registering a user 
    })
}

If the new account was created, the user is signed in automatically. – Firebase Ref

The above code returns the user data as current user. So the second scenario fails (even an admin logged in, once they have created a user, it’s automatically changing the admin states to new user states). So I create a new user through back-end.

In Spring Boot

CreateRequest request = new CreateRequest().setEmail(user.getEmail()).setPassword(user.getPassword());
UserRecord userRecord = FirebaseAuth.getInstance().createUser(request);

This is creating successfully a user without changing logged-in user’s states.

Is there any way to send a email verification to the second scenario (through back-end or front-end)?

Solution

Well, I updated latest dependency in back-end (Spring-boot) which has generateEmailVerificationLink();

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>6.8.1</version>
</dependency>

So the verification mail can be generated and sent through custom mail service. Generate verification link

// Generating verification link with the help of firebase
String link=FirebaseAuth.getInstance().generateEmailVerificationLink(user.getEmail());

// Sending the link through custome mail service
emailService.sendMail("Your mail id", user.getEmail(), "Verfication email", link);

Answered By – varman

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.