首页 > 解决方案 > 在 Kotlin 的 Firebase Auth 中获取当前登录用户的 ID

问题描述

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

标签: androidkotlinfirebase-authentication

解决方案


获取当前登录用户的 Firebase 文档中:

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

然后,您可以从有关获取用户配置文件的文档中获取他用户的 UID,user.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
}

推荐阅读