在Firebase Auth中,如何检测到匿名用户被UPGRADED到例如Facebook的用户? [英] In Firebase Auth, how to detect that an anonymous user was UPGRADED to an e.g. Facebook user?

查看:77
本文介绍了在Firebase Auth中,如何检测到匿名用户被UPGRADED到例如Facebook的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序中有一个单例,是在应用程序启动时启动的,它监听auth状态的更改,如下所示:

I have a singleton in my Android app, started at startup of the app, that listens to auth state changes like so:

fun listenForAuthChanges() {
    if (authStateListener != null) {
        FirebaseAuth.getInstance().removeAuthStateListener(authStateListener!!)
    }
    authStateListener = FirebaseAuth.AuthStateListener { auth ->
        val user = auth.currentUser
        writeStatusToFirebase()
        if (user != null) {
            Log.debug("User : ${user.uid} -> ${user.loginType}")
        } else {
            Log.debug("User : signed out")
            loginAnonymously()
        }
    }
    FirebaseAuth.getInstance().addAuthStateListener(authStateListener!!)
}

这非常好用,可以检测使用是否已注销或已登录.如您在上面的代码中使用loginAnonymously()所看到的,当没有用户登录时,我会自动匿名登录.这种方式的作用就像一种魅力,但是......当我调用Firebase-UI进行登录并且用户通过Facebook登录时,不会调用Auth状态侦听器.

This works perfectly, detecting if a use is logged out, or logged in. As you can see in the code above using the loginAnonymously(), when there is no user logged-in then I automatically login anonymously. This al works like a charm, however.... when I call the Firebase-UI to login and the user logs in via Facebook the Auth state listener is not called.

我发现FirebaseUI实际上并没有创建新用户,而是将匿名用户升级为Facebook用户(在Firebase Auth控制台中检查并通过使用Android Studio控制台中的断点进行检查).这实际上是我想要的行为.

I figured out that FirebaseUI actually does not create a new user, instead the anonymous user is upgraded to Facebook user (checked in the Firebase Auth console and by using breakpoints in the Android studio console). This is actually the behaviour that I want.

所以我想,结论是由于用户的uid不变,未调用Auth状态侦听器?

So I guess, the conclusion is that the Auth state listener is not called because the user's uid does not change?

但是,我确实需要一种可靠的方式来检测此事件(这意味着用户已从匿名升级到例如Facebook).

However, I do need a reliable way to detect also this event (meaning user upgraded from anonymous to e.g. Facebook).

做到这一点的最佳方法是什么?

What would be the best way to do this?

注意:我知道可以检测到用户正在使用哪种身份验证提供程序(id)进行身份验证. 这是我使用的一些代码:

Note: I know its possible to detect what auth provider (id) a user is using to authenticate. Here is a bit of code I use for this:

val FirebaseUser.loginType: LoginType

get() {
    if (isAnonymous) {
        return LoginType.Anonymous
    }
loop@ for (userInfo in providerData) {
    when (userInfo.providerId) {
        EmailAuthProvider.PROVIDER_ID -> return LoginType.Email
        GoogleAuthProvider.PROVIDER_ID -> return LoginType.Google
        FacebookAuthProvider.PROVIDER_ID -> return LoginType.Facebook
        else -> continue@loop
    }
}
return LoginType.Unknown

}

这不是我的问题,我的问题是:如何检测用户已升级从匿名用户升级到例如Facebook的?

That is not my issue, my questions is: how to detect that a user has been upgraded from anonymous to e.g. Facebook?

推荐答案

如何检测用户已从匿名升级到例如Facebook的?

how to detect that a user has been upgraded from anonymous to e.g. Facebook?

匿名用户首次登录时,将身份验证类型保存在数据库中:

When an anonymous user signs in for the first time, save the authentication type in the database:

Firestore-root
  |
  --- users (collection)
        |
        --- uid
             |
             --- type: "anonymous"

当用户更改身份验证类型时,您只需更改数据库中的type字段即可保留"Facebook"而不是"anonymous".这是可能的,因为无论提供者ID是什么,uid都将始终相同.要在操作发生时得到通知,只需在type属性上附加一个侦听器,即可实时收到通知.

When a user is changing the authentication type, you should simply change the type field in the database to hold "Facebook" instead of "anonymous". This is possible because the uid will always be the same, no matter what the provider id. To be notified when the operation takes place, simply attach a listener on the type property and you'll be notified in real-time.

这篇关于在Firebase Auth中,如何检测到匿名用户被UPGRADED到例如Facebook的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆