Email verification for Firebase Auth UI

前端 未结 2 1110
不思量自难忘°
不思量自难忘° 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 */
        }
    }
    

提交回复
热议问题