Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email verification for Firebase Auth UI

I am using firebase auth UI (FirebaseUI-Android) in an android app, where the user can signup with personal email, Facebook, number and Gmail accounts. My question is I need to get email verification when user sign's up with his personal email id.

    List<AuthUI.IdpConfig> providers = Arrays.asList(
        new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
        new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build(),
        new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build(),
        new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build());

    startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setIsSmartLockEnabled(true)
                .setTheme(R.style.GreenTheme)
                .setTosUrl("https://termsfeed.com/blog/terms-conditions-mobile-apps/")
                .setPrivacyPolicyUrl("https://superapp.example.com/privacy-policy.html")
                .setAvailableProviders(providers)
                .build(),
        RC_SIGN_IN);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // RC_SIGN_IN is the request code you passed into startActivityForResult(...) when starting the sign in flow.
    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
        // Successfully signed in
        if (resultCode == RESULT_OK) {
            startActivity(new Intent(Login.this,MainActivity.class));
            finish();
            return;
        } else {
            // Sign in failed
            if (response == null) {
                Toasty.error(getApplicationContext(),"Sign in cancelled",Toast.LENGTH_SHORT, true).show();
                return;
            }

            if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
                Toasty.error(getApplicationContext(),"No internet connection",Toast.LENGTH_SHORT, true).show();
                return;
            }

            if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
                Toasty.error(getApplicationContext(),"Unkown Error",Toast.LENGTH_SHORT, true).show();
                return;
            }
        }
        Toasty.error(getApplicationContext(),"Unknown sign in response",Toast.LENGTH_SHORT, true).show();
    }
}  

Here is my intent for sign up options.

enter image description here

like image 357
ravi Avatar asked Feb 26 '18 22:02

ravi


1 Answers

@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)

like image 86
mayank1513 Avatar answered Oct 04 '22 16:10

mayank1513