【问题标题】:What is the purpose of this code in Firebase auth?Firebase 身份验证中此代码的目的是什么?
【发布时间】:2018-11-22 08:59:55
【问题描述】:

我查看了 FirebaseUI 登录方法。在解释的第一页有一个“使用预构建的 UI 登录”,如下所示:Easily add sign-in to your Android app with FirebaseUI。该页面包含以下代码:

// Choose authentication providers
List<AuthUI.IdpConfig> providers = Arrays.asList(
        new AuthUI.IdpConfig.EmailBuilder().build(),
        new AuthUI.IdpConfig.PhoneBuilder().build(),
        new AuthUI.IdpConfig.GoogleBuilder().build(),
        new AuthUI.IdpConfig.FacebookBuilder().build(),
        new AuthUI.IdpConfig.TwitterBuilder().build());

// Create and launch sign-in intent
startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .build(),
        RC_SIGN_IN);

然后

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

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

        if (resultCode == RESULT_OK) {
            // Successfully signed in
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            // ...
        } else {
            // Sign in failed. If response is null the user canceled the
            // sign-in flow using the back button. Otherwise check
            // response.getError().getErrorCode() and handle the error.
            // ...
        }
    }
}

但是,当我查看他们关于如何通过 SMS 使用电话进行身份验证的示例时,无法看到此代码,但验证有效。 手机验证码可以在他们的sn-p中看到: PhoneAuthActivity.java

那么我什么时候需要使用这段代码,什么时候不需要呢?这部分的目的是什么?

【问题讨论】:

    标签: android firebase firebase-authentication


    【解决方案1】:

    有两种主要的方式来实现 Firebase 身份验证:

    1. 使用所谓的无头 API 并在此基础上构建您自己的 API 和流程。
    2. 使用 FirebaseUI,它是一个 UI 库,(在这种情况下)封装了 Firebase 身份验证登录流程。正如您在 its open source implementation 中看到的那样,它在后台调用 Firebase 身份验证 API。

    您展示的第一个 sn-p 配置 FirebaseUI,具体说明启用了哪些提供程序(Google、Facebook、电子邮件+密码、电话、Twitter),然后启动一个活动以启动流程。

    第二个 sn-p 直接使用 Firebase 身份验证 API 来实现部分身份验证流程。

    如果您使用的是 FirebaseUI,请关注其存储库中的 documentation for configuring phone number auth。据我所见,这不需要您共享的onActivityResult。该流程很可能已经封装在 FirebaseUI 本身中。

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 1970-01-01
      • 2019-04-19
      • 2020-11-12
      • 2020-04-22
      • 2016-01-29
      • 2020-01-28
      • 2021-12-20
      • 2016-04-22
      相关资源
      最近更新 更多