2017-05-20 9 views
0

我在我的应用程序中使用FirebaseUI-auth以便使用电子邮件和Facebook登录用户。如何在使用FirebaseUI-auth时验证电子邮件而无需再次登录?

当用户使用电子邮件登录时,他被带到一个显示文本的屏幕,要求他验证他的电子邮件。

这里是我的代码:

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(); 
     } 
    } 

当他点击发送给他的验证链接,他的电子邮件得到验证,但现在,当他回来的应用程序,或者当他关闭并重新打开应用程序时,屏幕仍然显示验证电子邮件的文本,以及在验证电子邮件后,他何时注销并再次登录,才允许他访问该应用程序。

这里的,其确定电子邮件验证或不代码:

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(); 
            } 
           } 
          }); 

       } 
      } 
     } 

这里的onResume()

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

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

    } 

我想知道的是我怎么可以给用户完全访问该应用什么没有他登出并在他的电子邮件验证后再次登录?

请让我知道。

回答

1

在currentUser上调用reload()应该更新isEmailVerified()状态。所以你可以做的一件事是当应用程序恢复时,重新加载()用户,检查是否验证,如果没有,请求用户验证他们的帐户,否则让用户继续使用应用程序。通过这样做,用户不需要每次都重新登录。

+0

请参阅我在更新后的问题中的'onResume()'中添加的代码。它仍然不起作用。 –

+1

重新加载可能不一定会触发onAuthStateChanged侦听器。重新加载后,您必须检查用户是否已验证,然后根据需要发送电子邮件。 – bojeil

相关问题