Email verification using Firebase 3.0 on Android

后端 未结 4 1409
我寻月下人不归
我寻月下人不归 2020-11-30 12:34

I knew that we can verify the users email with Firebase Auth 3.0. I\'m not able to find any documentation regarding email verification on Android. I\'m able to find the same

相关标签:
4条回答
  • 2020-11-30 13:12

    Email verification for android is now available in Firebase. See this release note: https://firebase.google.com/support/release-notes/android#9.6

    0 讨论(0)
  • 2020-11-30 13:18

    Update

    Email verification is available in version 9.6 and higher of the Firebase SDK for Android.

    Original answer

    Email verification is not available for Android yet. Also answered here with more context.

    0 讨论(0)
  • 2020-11-30 13:20

    An alternative suggested by the Firebase team

    One thing you could do is to add a node to your Firebase Database which contains all email addresses as children. You should make this node only publicly readable (via Firebase security rules).

    Then from within your apps, once a user signs up / signs in, you check if the email of that user is on the list, and if not, you sign them out and kick them out of your app (and as a bonus, you could even log the intruder's email address in your database, so you can later check who is trying to access your app).

    This will work for initial testing if you know the e-mail ids of the people who are gonna test your app until the e-mail verification makes its way to Android.

    0 讨论(0)
  • 2020-11-30 13:26

    Since email verification only works with Email/Password authentication, the best place to send it wold be in the onComplete method of createUserWithEmailAndPassword(...) method, after signup is successful.

    firebaseAuth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                sendVerificationEmail();
                             ....
    

    The custom sendVerification method is:

    public void sendVerificationEmail() {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    
        if (user != null) {
            user.sendEmailVerification()
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(SignUpActivity.this, "Signup successful. 
                                    Verification email sent", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }
    
    }
    

    You can then check if the user has verified their email anywhere in your app by calling:

    mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
              firebaseUser = firebaseAuth.getCurrentUser();
                if (firebaseUser != null ) {
                    Log.e(TAG, firebaseUser.isEmailVerified() ? "User is signed in and email is verified" : "Email is not verified");
                } else {
                    Log.e(TAG, "onAuthStateChanged:signed_out");
                }
            }
        };
    
    0 讨论(0)
提交回复
热议问题