【问题标题】:String value of userid of firebase auth to document firestore [duplicate]Firebase身份验证的用户ID的字符串值以记录Firestore [重复]
【发布时间】:2020-03-06 05:27:06
【问题描述】:

你能帮助我的 Android 项目吗?我正在尝试在 firebase auth 中获取 currentUID,我希望它在我的 firestore 文档中显示字符串值。

db2.collection("Assessment").document(??).set(answer);

【问题讨论】:

标签: android firebase google-cloud-firestore


【解决方案1】:

当前登录用户的ID可以通过FirebaseUser实例的getUid方法获取,可以通过FirebaseAuth#getCurrentUser获取:

Java:

FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
String uid = user.getUid();
// Or alternatively, a one-liner (although the code complexity is higher)
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

科特林:

val auth = FirebaseAuth.getInstance()
val user = auth.currentUser // Note: This returns a nullable class
val uid = user?.uid
// Or alternatively, a one-liner (although the code complexity is higher)
val uid = FirebaseAuth.getInstance().currentUser?.uid

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 2021-10-02
    • 2021-05-29
    • 2021-06-11
    • 2021-07-31
    • 1970-01-01
    • 2021-06-23
    • 2022-12-08
    相关资源
    最近更新 更多