1

How should I go about verifying an email address prior to the user signing up with Firebase? I know that an email address is verified with .sendEmailVerification, however this only works on the current user. Hence a user must be already created before sending a verification email. This would not be of much help since you obviously have to verify an email before adding it to your database. Therefore, what is a good workaround?

dre_84w934
  • 678
  • 8
  • 27

1 Answers1

2

You can't verify the email prior to sign up with Firebase Auth. Email verification is not always required. This is why Firebase Auth provides it as a method on the user. Some applications do not require email verification on sign-up, others may make it optional, others may offer limited access to unverified users, etc.

If you want to require users to be verified before accessing your app content, you can either: enforce that via Firebase rules, eg: ".read": "auth.token.email_verified === true"

Or, if you are using your own backend, use the Firebase Admin SDK, https://firebase.google.com/docs/auth/admin/verify-id-tokens:

admin.auth().verifyIdToken(idToken).then(decodedToken => {
  if (decodedToken.email_verified) {
    // Email verified. Grant access.
  } else {
    // Email not verified. Ask user to verify email.
  }
});
bojeil
  • 29,642
  • 4
  • 69
  • 76
  • 1
    So if the to be user decides to cancel sign up, just before I have already created its user in Firebase in order to verify its email, what do I do with its already-created-and-not-verified account? I know the answer is whatever I want ;) but what is the best approach in case the user might come back with the same email address? – dre_84w934 Dec 29 '17 at 22:58
  • You can write your own cleanup script. Firebase Admin SDK provide APIs to `listUsers` (lookup all users) and `deleteUser` (delete a user by `uid`). You can have a recurring script that lists all the users and checks if `emailVerified` is false and creation time is too old, then, delete those users. – bojeil Jan 02 '18 at 00:49