问题描述
我正在使用 FirebaseUI-auth 在我的应用中,以便使用电子邮件和Facebook登录用户.
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.
这是我的代码:
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();
}
}
});
}
}
}
这里是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?
请让我知道.
推荐答案
在currentUser上调用reload()应该更新isEmailVerified()状态.因此,您可以做的一件事是当应用恢复时,重新加载用户,检查是否已通过验证,如果没有通过验证,请用户验证其帐户,否则让用户继续使用该应用.这样,用户将不需要每次都重新登录.
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.
这篇关于使用FirebaseUI-auth时,如何在无需再次登录的情况下验证电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!