gpt4 book ai didi

java - Firebase Auth UI 的电子邮件验证

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:21 30 4
gpt4 key购买 nike

我在 Android 应用程序中使用 firebase auth UI ( FirebaseUI-Android ),用户可以在其中使用个人 emailFacebook 进行注册、numberGmail 帐户。我的问题是,当用户使用他的个人电子邮件 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();
}
}

这是我对注册选项的 Intent 。

enter image description here

最佳答案

你可以简单地按照下面的方式来做,

  1. 获取当前的 Firebase 用户实例,

    final FirebaseUser currentUser = mAuth.getCurrentUser();
  2. 检查provider是否为password说明使用的登录方式为Email Auth,

    if(null != currentUser) {
    if("password".equals(currentUser.getProviderData().get(0).getProviderId())) {
    /* Handle Verification */
    }
    }

    引用链接:https://firebase.google.com/docs/reference/android/com/google/firebase/auth/EmailAuthProvider#PROVIDER_ID

  3. 检查用户是否已经过验证,

    currentUser.isEmailVerified();
  4. 如果用户未通过验证,则可以使用以下代码发送验证电子邮件,

    if (!currentUser.isEmailVerified()) {
    /* Do Something */
    }

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

一旦你准备好了所有的部分,最终的代码 fragment 应该如下所示:

最终代码 fragment :

if (requestCode == RC_SIGN_IN) {
IdpResponse response = IdpResponse.fromResultIntent(data);

/* Success */
if (resultCode == RESULT_OK) {
final FirebaseUser currentUser = mAuth.getCurrentUser();

if(null != currentUser) {
if("password".equals(currentUser.getProviderData().get(0).getProviderId())) {
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 */
}
}

关于java - Firebase Auth UI 的电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48998161/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com