firebase admin SDK create user and send verification email

passion picture passion · Jan 26, 2017 · Viewed 8.9k times · Source

How can I send a verification email after I create a user with firebase admin SDK? I am trying to combine createUser function and sendEmailVerification function could somebody indicate a hint or an answer? thanks

update:

the user creation is being done by an admin user who is already signed in in the app, so the admin user is just creating users on the dashboad. This is completely different from the registeration methods.

update 2:

I tried to follow bojeil's answer, I am still stuck with the step that user signs in with the custom token. It gets into conflict with my current admin user session, the admin users gets kicked out and instead the new user is signed in and even when I sign out the new user, the admin user is still out and needs to sign in to get back into the app.

here is my code inside the app after I get the custom token:

$http.post('/.custom-token', {uid: $scope.data.data.uid})
        .then(function (response) {
            console.log("custom token here:", response.data.token);
            firebase.auth().signInWithCustomToken(response.data.token)
                .then(function (firebaseUser) {
                    firebaseUser.sendEmailVerification();
                    firebase.auth().signOut().then(function() {
                        // Sign-out successful.
                        console.log("signed out success");
                    }, function(error) {
                        // An error happened.
                    });
                })

                .catch(function(error) {
                    // Handle Errors here.
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    // ...
                });

        });

so, I get the token, sign in the new user, send the email verification link, and then, sign out the new user. But my admin user who is doing all of this gets signed out as well. what am I missing here?

Answer

bojeil picture bojeil · Jan 27, 2017

OK this is what you can do but you may hit quota limitations:

  • Include the firebase-admin module.
  • Include the firebase client module.
  • using admin sdk, create the new user via createUser
  • when that promise resolves, get the uid of the user created.
  • using admin sdk, create custom token for that uid.
  • using client sdk, signInWithCustom token using that custom token.
  • A user is returned in the process, call user.sendEmailVerification()
  • signOut that user from the client SDK.