在 Kotlin 的 Firebase Auth 中获取当前登录的用户 ID

Ric*_*ard 0 android kotlin firebase-authentication

如何使用 Firebase Auth 在 Kotlin 中获取当前登录的用户 ID?我想拿这个 ID 来检索所述用户的完整信息以显示在一个 Activity 中。

Fra*_*len 5

从有关获取当前登录用户的 Firebase 文档

val user = FirebaseAuth.getInstance().currentUser
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以user.uid获取用户个人资料的文档中获取用户的 UID,如本示例所示:

val user = FirebaseAuth.getInstance().currentUser
user?.let {
    // Name, email address, and profile photo Url
    val name = user.displayName
    val email = user.email
    val photoUrl = user.photoUrl

    // Check if user's email is verified
    val emailVerified = user.isEmailVerified

    // The user's ID, unique to the Firebase project. Do NOT use this value to
    // authenticate with your backend server, if you have one. Use
    // FirebaseUser.getToken() instead.
    val uid = user.uid
}
Run Code Online (Sandbox Code Playgroud)