How to verify email without the need of signing in

2019-08-18 23:03发布

I'm using FirebaseUI-auth in my app in order to sign in user using email and facebook.

When a user signin using email he is taken to a screen which shows a text asking him to verify his email.

Here's my code:

    continue_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivityForResult(
                            // Get an instance of AuthUI based on the default app
                            AuthUI.getInstance()
                                    .createSignInIntentBuilder()
                                    .setProviders(Arrays.asList(new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
                                            new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build()))
                                    .setIsSmartLockEnabled(!BuildConfig.DEBUG)
                                    .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 == ResultCodes.OK) {
                Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                return;
            } else {
                // Sign in failed
                if (response == null) {
                    // User pressed back button
                    Toast.makeText(getBaseContext(), "Sign in cancelled", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
                    Toast.makeText(getBaseContext(), "No internet", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
                    Toast.makeText(getBaseContext(), "Unknown error", Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            Toast.makeText(getBaseContext(), "Unknown sign in response", Toast.LENGTH_SHORT).show();
        }
    }

When he clicks on the verification link sent to him, his email gets verified but now when he comes back to the app or when he close and reopens the app, the screen still shows the text to verify the email and when he logs out and sign in again after verifying the email then only he is allowed to have access of the app.

Here's the code which determines if email is verified or not:

if (auth.getCurrentUser() != null) {
            if (auth.getCurrentUser().getProviders().contains("password")) {

                boolean emailVerified = auth.getCurrentUser().isEmailVerified();

                if (emailVerified) {

                    Toast.makeText(getBaseContext(), "Email has been verified", Toast.LENGTH_SHORT).show();
                    emailNotVerifiedTxt.setVisibility(View.INVISIBLE);
                    openEmailBtn.setVisibility(View.INVISIBLE);

                } else {

                    emailNotVerifiedTxt.setVisibility(View.VISIBLE);
                    openEmailBtn.setVisibility(View.VISIBLE);
                    auth.getCurrentUser().sendEmailVerification()
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        Snackbar snackbar = Snackbar
                                                .make(coordinatorLayout, "An email verification link has been sent to " + auth.getCurrentUser().getEmail(), Snackbar.LENGTH_LONG);
                                        snackbar.show();
                                    }
                                }
                            });

                }
            }
        }

Here's onResume():

@Override
    protected void onResume() {
        super.onResume();

        if (auth != null) {
            if (auth.getCurrentUser() != null) {
                auth.getCurrentUser().reload();
            }
        }

    }

What I want to know is how can I give the user full access to the app without him logging out and signing in again once his email is verified?

Please let me know.

1条回答
别忘想泡老子
2楼-- · 2019-08-18 23:50

Calling reload() on the currentUser should update isEmailVerified() status. So one thing you can do is when the app resumes, reload() the user, check if verified, if not, ask the user to verify their account, otherwise let the user proceed to use the app. By doing so, the user will not need to sign in again each time.

查看更多
登录 后发表回答