Email verification for Firebase Auth UI

前端 未结 2 1131
不思量自难忘°
不思量自难忘° 2021-02-20 01:27

I am using firebase auth UI (FirebaseUI-Android) in an android app, where the user can signup with personal email, Facebook, number<

相关标签:
2条回答
  • 2021-02-20 01:36

    You can simply do it as follows,

    1. Get Current Firebase User Instance,

      final FirebaseUser currentUser = mAuth.getCurrentUser();
      
    2. Check if the provider is password indicating that the login method used is Email Auth,

      if(null != currentUser) {
          if("password".equals(currentUser.getProviderData().get(0).getProviderId())) {
              /* Handle Verification */
          }
      }
      

      Reference Link: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/EmailAuthProvider#PROVIDER_ID

    3. Check if user is already verified,

      currentUser.isEmailVerified();
      
    4. If user is not verified then the following code can be used to send a verification EMail,

      if (!currentUser.isEmailVerified()) {
          /* Do Something */
      }
      
      /* Send Verification Email */
      currentUser.sendEmailVerification()
              .addOnCompleteListener(this, new OnCompleteListener() {
                  @Override
                  public void onComplete(@NonNull Task task) {
                      /* Check Success */
                      if (task.isSuccessful()) {
                          Toast.makeText(getApplicationContext(),
                                  "Verification Email Sent To: " + currentUser.getEmail(),
                                  Toast.LENGTH_SHORT).show();
                      } else {
                          Log.e(TAG, "sendEmailVerification", task.getException());
                          Toast.makeText(getApplicationContext(),
                                  "Failed To Send Verification Email!",
                                  Toast.LENGTH_SHORT).show();
                      }
                  }
              });
      

    Once you have all the pieces in place, the final code snippet should look something like below:

    Final Code Snippet:

    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
    
        /* Success */
        if (resultCode == RESULT_OK) {
            final FirebaseUser currentUser = mAuth.getCurrentUser();
    
            if(null != currentUser) {
                if("password".equals(currentUser.getProviderData().get(0).getProviderId())) {
                    if(!currentUser.isEmailVerified()) {
                        /* Send Verification Email */
                        currentUser.sendEmailVerification()
                            .addOnCompleteListener(this, new OnCompleteListener() {
                                @Override
                                public void onComplete(@NonNull Task task) {
                                    /* Check Success */
                                    if (task.isSuccessful()) {
                                        Toast.makeText(getApplicationContext(),
                                                "Verification Email Sent To: " + currentUser.getEmail(),
                                                Toast.LENGTH_SHORT).show();
                                    } else {
                                        Log.e(TAG, "sendEmailVerification", task.getException());
                                        Toast.makeText(getApplicationContext(),
                                                "Failed To Send Verification Email!",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
    
                        /* Handle Case When Email Not Verified */
                    }
                }
    
                /* Login Success */
                startActivity(new Intent(Login.this, MainActivity.class));
                finish();
                return;
            }
        } else {
            /* Handle Failure */
        }
    }
    
    0 讨论(0)
  • 2021-02-20 01:48

    @user2004685 answer above gives very good hint. But it does not work at least for the latest firebase-ui because currentUser.getProviderData().get(0).getProviderId() returns 'firebase'.

    So the updated solution is

    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
      if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
    
        /* Success */
        if (resultCode == RESULT_OK) {
            final FirebaseUser currentUser = mAuth.getCurrentUser();
    
            if(null != currentUser) {
                if(currentUser.getEmail()!=null) {
                    if(!currentUser.isEmailVerified()) {
                        /* Send Verification Email */
                        currentUser.sendEmailVerification()
                            .addOnCompleteListener(this, new OnCompleteListener() {
                                @Override
                                public void onComplete(@NonNull Task task) {
                                    /* Check Success */
                                    if (task.isSuccessful()) {
                                        Toast.makeText(getApplicationContext(),
                                                "Verification Email Sent To: " + currentUser.getEmail(),
                                                Toast.LENGTH_SHORT).show();
                                    } else {
                                        Log.e(TAG, "sendEmailVerification", task.getException());
                                        Toast.makeText(getApplicationContext(),
                                                "Failed To Send Verification Email!",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
    
                        /* Handle Case When Email Not Verified */
                    }
                }
    
                /* Login Success */
                startActivity(new Intent(Login.this, MainActivity.class));
                finish();
                return;
            }
        } else {
            /* Handle Failure */
        }
      }
    }
    

    simply replace if("password".equals(currentUser.getProviderData().get(0).getProviderId())) with if(currentUser.getEmail()!=null)

    0 讨论(0)
提交回复
热议问题