如果用户在 FirebaseUI Auth 中注销或撤销了提供商的访问权限,如何得到通知?

How to be informed if user signed out or revoked access from provider in FirebaseUI Auth?

我正在开发一个可以使用 Twitter 和电子邮件登录的 android 应用程序。我实现了 com.firebaseui:firebase-ui-auth:5.0.0。然后我检查用户是否已经登录,如果没有开始登录 activity 提供的 FirebaseUI Auth:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    firebaseAuth = FirebaseAuth.getInstance();


    List<AuthUI.IdpConfig> providers = Arrays.asList(
            new AuthUI.IdpConfig.EmailBuilder().build(),
            new AuthUI.IdpConfig.TwitterBuilder().build());


    if (firebaseAuth.getCurrentUser() == null) {
        startActivityForResult(
                AuthUI.getInstance()
                        .createSignInIntentBuilder()
                        .setAvailableProviders(providers)
                        .setIsSmartLockEnabled(true)
                        .build(),
                RC_SIGN_IN);
    } else {

        for (UserInfo profile : firebaseAuth.getCurrentUser().getProviderData()) {
            if (profile.getProviderId().equals(TwitterAuthProvider.PROVIDER_ID)) {
                // UID specific to the provider
                String uid = profile.getUid();

                String name = profile.getDisplayName();
                String email = profile.getEmail();
                Uri photoUrl = profile.getPhotoUrl();
            }
        }
        startActivity(new Intent(this,MainActivity.class));
}

通过这种方法,我可以使用我的 Twitter 帐户登录并声明我的基本 Twitter 信息,如姓名、电子邮件、照片 URL。 但是当我转到原始 Twitter android 应用程序 > 设置 > 帐户 > 应用程序和会话并撤销对我的应用程序的访问权限 然后再次尝试打开我的应用程序时,我仍然可以索取我的推特信息。 我想知道用户何时撤销了对我的应用程序的访问权限,以及每当他们这样做时,我希望他们注销。 我想知道如何克服这个问题,我也想知道这是否是针对这种情况的最佳做法。提前致谢。

在 Firebase 身份验证期间,会生成一个令牌以确保安全。一旦令牌被提供给用户,它就会一直有效,直到它过期。如果您从您的 Twitter 帐户撤销访问权限,并不意味着令牌已过期。这只是意味着如果用户注销,他将不能能够再次使用 Twitter 帐户进行另一次身份验证,因为访问权限已被撤销。不幸的是,您无法远程强制用户注销,因为您也不可能以某种方式访问​​用户的设备并删除令牌。任何注销都需要在用户登录的设备上进行。

因此,即使您从 Firebase 控制台禁用了用户的帐户,该用户仍可继续访问长达一个小时。如果这不是您想要的,有一个解决方法,您可以在 Cloud Firestore 或 Firebase 实时数据库中添加和维护 "revoked" 用户的列表,然后使用 Firebase Security Rules 进行检查。

例如,在实时数据库中,如果用户 ID 可能如下所示,则已撤销列表:

revoked
  |
  --- uidOne: true
  |
  --- uidTwo: true

然后相应的安全规则可能如下所示:

".read": "auth.uid !== null && !root.child('revoked').child(auth.uid).exists()"