Firebase admin SDK creates a user and sends a confirmation email

How can I send a confirmation email after creating a user using the firebase SDK? I am trying to combine createUser function and sendEmailVerification function can anyone provide a hint or answer? thanks

update:

user creation is performed by an administrator user who is already registered in the application, so the administrator user simply creates users in dashboad. This is completely different from registration methods.

update 2:

I tried to execute a bojeil answer , I still stick to the fact that the user is signing up with a user token. It conflicts with my current administrator session, admin users exit the game, and instead a new user logs in, and even when I log out a new user, the admin user still does not work and he needs to log in to return to the application.

here is my code inside the application after receiving a 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 a token, sign a new user, send a verification link by email, and then log out of the new user. But my administrator, who does all this, is also billing. What am I missing here?

+3
angularjs firebase firebase-authentication firebase-admin
source share
4 answers

OK, this is what you can do, but you may run into quota restrictions:

  • Enable the firebase-admin module.
  • Enable the firebase client module.
  • using admin sdk, create a new user via createUser
  • when this promise is resolved, get the uid of the created user.
  • using admin sdk, create a custom token for this uid.
  • using client sdk, signInWithCustom using this custom token.
  • The user returns to the process, calls user.sendEmailVerification ()
  • signOut this user from the client SDK.
+6
source share

A reasonably clean solution is to use the REST API.

 curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key=[API_KEY]' \ -H 'Content-Type: application/json' \ --data-binary '{"requestType":"PASSWORD_RESET","email":"[ user@example.com ]"}' 

[API KEY] is the api key for the web client, which can be extracted from Project Settings> add a app >> click web, and you will get the configuration with JSON, there is an APIKey in JSON that you need to use.

+1
source share

You donโ€™t even need to use the Firebase Admin SDK for this. You can simply use the standard client-side Firebase package:

 firebase.auth().createUserWithEmailAndPassword(email, password) .then(function(user) { console.log("User successfully created:", user.uid); return user.sendEmailVerification(); }) .then(function() { console.log("Email verification email successfully sent!"); }) .catch(function(error) { console.log("Error:", error); }); 
0
source share

According to firebase, admin-sdk does not currently support this feature. See their answer here: fooobar.com/questions/845624 / ...

Whenever an email / password authentication user logs in and tries to use a function that requires authentication, I call onAuthStateChanged () and then check the user record for verification by email.

If the letter has not been verified, and I have not yet sent an email with confirmation, I will send it automatically. I am returning an error asking the user to check their email. (I save the variable in the profile setting for the user in firestore to indicate whether it was sent earlier).

In the event of future attempts to use the application, if the message is still not verified, I return the same error, and also turn on the button in the error with the inscription "resend confirmation email", which starts sending a confirmation email when pressed, (Thus, I I donโ€™t automatically send tons of test emails every time a user tries to do something.)

0
source share

All Articles