Details on coding for email verification can be found in the Manage Users
section of the Firebase documentation (
Android,
iOS,
Web). For the
rest of this post, I'll be looking at the Android code, but the implementation
on the other platforms is very similar.
The core functionality is found on the
FirebaseUser
object. On it is a sendEmailVerification() method, which returns as
Task
used to asynchronously send the email, and report on the status. If the task is
successful, you know the email was sent. Here's an example:
final FirebaseUser user = mAuth.getCurrentUser();
user.sendEmailVerification()
.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
// Re-enable button
findViewById(R.id.verify_email_button).setEnabled(true);
if (task.isSuccessful()) {
Toast.makeText(EmailPasswordActivity.this,
"Verification email sent to " + user.getEmail(),
Toast.LENGTH_SHORT).show();
} else {
Log.e(TAG, "sendEmailVerification", task.getException());
Toast.makeText(EmailPasswordActivity.this,
"Failed to send verification email.",
Toast.LENGTH_SHORT).show();
}
}
});
In this case, the user is retrieved from
mAuth.getCurrentUser()
,
and the
sendEmailVerification()
method is called on it. This gets
Firebase to send the mail -- and if the task was successful, you'll see that a
toast will pop up showing that it happened.
After the user clicks the link in the email, subsequent calls to
user.isEmailVerified()
will return true. Here's how it is used in the sample app:
mStatusTextView.setText(getString(R.string.emailpassword_status_fmt,
user.getEmail(), user.isEmailVerified()));
Do note that the FirebaseUser object is cached within an app session, so if you
want to check on the verification state of a user, it's a good idea to call
.getCurrentUser().reload()
for an update.
In the sample app it just shows on the activity if the user's email address is
verified or not. For your app you could limit functionality based on this state
-- even to the extent of denying them access to the signed in section of the
app.
You can learn more about Firebase Auth, including sign-in with other providers
such as Google, Facebook, Twitter and more on the
Firebase developers
site.