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

时间:2019-11-30 14:53:16

标签: android kotlin firebase-authentication

如何使用Firebase Auth获取Kotlin中当前登录的用户ID?我想获取ID来检索要在活动中显示的用户的完整信息。

2 个答案:

答案 0 :(得分:2)

摘自getting the currently signed in user上的Firebase文档:

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

然后您可以从getting the user's profile的文档中获得本示例所示的用户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
}

答案 1 :(得分:0)

val currentUser = Firebase.auth.currentUser
currentUser?.let {

// Name, email address, and profile photo Url
val name = currentUser.displayName
val email = currentUser.email
val photoUrl = currentUser.photoUrl

// Check if user's email is verified
val emailVerified = currentUser.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 = currentUser.uid
}