Javascript 有没有办法获得 Firebase Auth 用户 UID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38352772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:22:39  来源:igfitidea点击:

Is there any way to get Firebase Auth User UID?

javascriptnode.jsapifirebasefirebase-authentication

提问by Piyush Bansal

I am looking to fetch Auth User(s) UID from Firebase via NodeJS or Javascript API. I have attached screenshot for it so that you will have idea what I am looking for.

我希望通过 NodeJS 或 Javascript API 从 Firebase 获取 Auth User(s) UID。我附上了它的屏幕截图,这样你就会知道我在找什么。

enter image description here

在此处输入图片说明

Hope, you guys help me out with this.

希望你们帮我解决这个问题。

回答by Dominic

Auth data is asynchronous in Firebase 3. So you need to wait for the event and then you have access to the current logged in user's UID. You won't be able to get the others. It will get called when the app opens too, if you're already logged in.

Firebase 3 中的身份验证数据是异步的。因此您需要等待事件,然后才能访问当前登录用户的 UID。你将无法得到其他人。如果您已经登录,它也会在应用程序打开时被调用。

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User logged in already or has just logged in.
    console.log(user.uid);
  } else {
    // User not logged in or has just logged out.
  }
});

Within your app you can either save this user object, or get the current user at any time with firebase.auth().currentUser.

在您的应用程序中,您可以保存此用户对象,也可以随时使用firebase.auth().currentUser.

https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged

https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged

回答by JamesD

if a user is logged in then the console.log will print out:

如果用户已登录,则 console.log 将打印出:

if (firebase.auth().currentUser !== null) 
        console.log("user id: " + firebase.auth().currentUser.uid);

回答by Karthi R

As of now in Firebase console, there is no direct API to get a list of users, Auth User(s) UID.
But inside your Firebase database, you can maintain the User UID at user level. As below,

截至目前,在 Firebase 控制台中,没有直接的 API 来获取用户列表,即 Auth User(s) UID。
但是在您的 Firebase 数据库中,您可以在用户级别维护用户 UID。如下,

"users": {
  "user-1": {
    "uid": "abcd..",
    ....
  },
  "user-2": {
    "uid": "abcd..",
    ....
  },
  "user-3": {
    "uid": "abcd..",
    ....
  }
}

Then you can make a query and retrieve it whenever you need uid's.
Hope this simple solution could help you!

然后,您可以进行查询并在需要 uid 时检索它。
希望这个简单的解决方案可以帮助你!

回答by Selfish

From Firebase docs, use Firebase.getAuth():

从 Firebase 文档中,使用Firebase.getAuth()

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
var authData = ref.getAuth();

if (authData) {
  console.log("Authenticated user with uid:", authData.uid);
}

Source:

来源: