Email verification for Firebase Auth UI

前端 未结 2 1074
不思量自难忘°
不思量自难忘° 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: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)

提交回复
热议问题