有没有办法获得 Firebase Auth 用户 UID? [英] Is there any way to get Firebase Auth User UID?

查看:55
本文介绍了有没有办法获得 Firebase Auth 用户 UID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过 NodeJS 或 Javascript API 从 Firebase 获取身份验证用户 UID.我附上了它的屏幕截图,这样你就会知道我在找什么.

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.

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

Hope, you guys help me out with this.

推荐答案

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

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.

如果您愿意,您也可以仅在接收到事件后渲染您的应用,以避免在那里使用额外的逻辑来确定事件是否已触发.

You can also render your app only once receiving the event if you prefer, to avoid extra logic in there to determine if the event has fired yet.

您还可以根据 user 的存在从这里触发路由更改,这与加载路由之前的检查相结合是确保只有合适的人查看 publicOnly 或 privateOnly 页面的可靠方法.

You could also trigger route changes from here based on the presence of user, this combined with a check before loading a route is a solid way to ensure only the right people are viewing publicOnly or privateOnly pages.

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.
  }
});

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

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

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

这篇关于有没有办法获得 Firebase Auth 用户 UID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆