我正在应用中使用 FirebaseUI-auth,以便使用电子邮件和 Facebook 登录用户。
当用户使用电子邮件登录时,他会被带到一个屏幕,该屏幕显示一条文本,要求他验证他的电子邮件。
这是我的代码:
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();
}
}
}
我想知道的是,一旦用户的电子邮件通过验证,我如何才能在不注销并重新登录的情况下授予对应用程序的完全访问权限?
请让我知道。
在当前用户上调用 reload(( 应该更新 isEmailVerified(( 状态。因此,您可以做的一件事是,当应用程序恢复时,重新加载((用户,检查是否通过验证,如果没有,请用户验证他们的帐户,否则让用户继续使用该应用程序。这样,用户就不需要每次都再次登录。