Firebase Auth:使用 FirebaseUI 登录后获取 AuthCredential-Android

Firebase Auth: Getting an AuthCredential after sign in using FirebaseUI-Android

我有一个 Android 应用程序,其中包含一些活动和长期 运行ning 服务。该服务有一些模型 类,存储在 Firebase 数据库中的 POJO。

首先 运行,该应用程序确定用户尚未登录并使用 FirebaseUI-Android 提供帐户选择器 activity 和 Google 和 Facebook 就可以了。这很常见:

    Intent signInIntent = AuthUI.getInstance().createSignInIntentBuilder()
            .setProviders(Arrays.asList(
                    new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
                    new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build()))
            .setTheme(R.style.AppThemeFirebaseAuth)
            .setLogo(R.drawable.logo)
            .setIsSmartLockEnabled(!BuildConfig.DEBUG)
            .build();

    activity.startActivityForResult(signInIntent, Request.FIREBASE_AUTH);

然后我可以获得 FirebaseUser 并开始使用数据库进行操作。

FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

DatabaseReference firebaseUserRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());

问题出现在服务的下一个 运行 上,可能是在没有 activity 的时候。我想再次进行一些数据库操作,但没有任何用户交互。看起来我需要调用 FirebaseAuth.signInWithCredential(),但我从哪里获得 AuthCredential 引用? AuthCredential 不会传回开始登录的 activity。

我可以使用 FirebaseUI 中的 Smart Lock 功能-Android 来帮忙吗?

你不能。 从这个线程 https://github.com/firebase/FirebaseUI-Android/issues/123

Ah, we have a slight problem here after discussing this with some colleagues. There is deliberately no way to get back an AuthCredential from an existing user, to avoid potentially leaking credentials. So, the linking of the authentication method to the anonymous account would have to occur within Firebase UI if you are using the library. I'll take a look at this next week to get you unblocked ASAP