【问题标题】:Can Phone Number and Email verfication both be applied in Firebase? [closed]电话号码和电子邮件验证都可以在 Firebase 中应用吗? [关闭]
【发布时间】:2019-02-10 06:14:29
【问题描述】:

我想制作一个应用程序,它使用 Firebase 接收用户的电子邮件 ID 和联系电话(除了一些其他字段)。之后,我想通过向用户的联系电话发送 OTP 并向用户的电子邮件 ID 发送验证电子邮件来验证用户的电子邮件 ID 和联系电话。可以在 Firebase 中完成吗?如果是这样,请指导我如何。我正在使用安卓工作室。

【问题讨论】:

标签: android firebase android-studio firebase-authentication


【解决方案1】:

我相信这是可能的,尽管它会有点棘手。您将必须同时实现 emailphone number 并在回调中添加一个检查以查看两者是否在用户登录之前都已完成。我认为鉴于电子邮件似乎必须让用户登录,您将不得不实施电话验证首先在电子邮件之前进行,并在检查电子邮件和登录之前添加一个电话已验证的检查。显示在文档中:

按照文档中的说明设置电话验证和回调

mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

    @Override
    public void onVerificationCompleted(PhoneAuthCredential credential) {
    // This callback will be invoked in two situations:
    // 1 - Instant verification. In some cases the phone number can be instantly
    //     verified without needing to send or enter a verification code.
    // 2 - Auto-retrieval. On some devices Google Play services can automatically
    //     detect the incoming verification SMS and perform verification without
    //     user action.
    Log.d(TAG, "onVerificationCompleted:" + credential);
    //signInWithPhoneAuthCredential(credential);   //send email for email verification here instead/ sign in with email method
    } 

这样确认电话号码后,可能会发送电子邮件验证

//Save the user email address beforehand for this

FirebaseAuth auth = FirebaseAuth.getInstance();
auth.sendSignInLinkToEmail(email, actionCodeSettings)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "Email sent.");
                }
            }
        });

实现此https://firebase.google.com/docs/auth/android/email-link-auth#completing_sign-in_in_an_android_app 以设置动态链接以在应用内完成登录 并有类似的东西用于使用电子邮件方法登录。

FirebaseAuth auth = FirebaseAuth.getInstance();
Intent intent = getIntent();
String emailLink = intent.getData().toString();

// Confirm the link is a sign-in with email link.
if (auth.isSignInWithEmailLink(emailLink)) {
    // Retrieve this from wherever you stored it
    String email = "someemail@domain.com";

    // The client SDK will parse the code from the link for you.
    auth.signInWithEmailLink(email, emailLink)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "Successfully signed in with email link!");
                        AuthResult result = task.getResult();
                        // You can access the new user via result.getUser()
                        // Additional user info profile *not* available via:
                        // result.getAdditionalUserInfo().getProfile() == null
                        // You can check if the user is new or existing:
                        // result.getAdditionalUserInfo().isNewUser()
                    } else {
                        Log.e(TAG, "Error signing in with email link", task.getException());
                    }
                }
            });
}

我认为有更好的方法来解决这个问题。也许控制台中有某种云功能,但这是我目前能找到的唯一方法。

【讨论】:

    猜你喜欢
    • 2020-04-05
    • 2018-05-05
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多