如何在firebase上获取当前用户

[英]how to get current user on firebase


I would like to know how how to get the current user. I am making a function where the user is creating a group and would like to add the user making the group to it at the same time. I can make the group fine, that was simple enough. But I do not know how to get to the user object outside of the simple login object.

我想知道如何获得当前用户。我正在创建一个用户正在创建一个组的功能,并希望同时添加组成组的用户。我可以让团队变得更好,这很简单。但我不知道如何到达简单登录对象之外的用户对象。

I'm sorry if there are several topics stating this already, but I have been looking for hours and have not been able to find anything that explains it. Any help would be appreciated.

我很抱歉,如果已经有几个主题说明了这一点,但我一直在寻找几个小时,但却找不到任何解释它的东西。任何帮助,将不胜感激。

1 个解决方案

#1


10  

The currently logged in user is returned from Simple Login's callback. This callback runs when your user authenticates, or if your user is already authenticated, it runs at the time of page load.

当前登录的用户从Simple Login的回调中返回。此回调在用户进行身份验证时运行,或者如果您的用户已经过身份验证,则会在页面加载时运行。

Take this code form the simple login docs:

从简单的登录文档中获取此代码:

var myRef = new Firebase("https://<your-firebase>.firebaseio.com");
var authClient = new FirebaseSimpleLogin(myRef, function(error, user) {
  if (error) {
    // an error occurred while attempting login
    console.log(error);
  } else if (user) {
    // user authenticated with Firebase
    console.log("User ID: " + user.uid + ", Provider: " + user.provider);
  } else {
    // user is logged out
  }
});

The user object is exposed in the callback. It's only in scope during the execution of that callback, so if you want to use it outside, store it in a variable for reuse later like this:

用户对象在回调中公开。它只在执行该回调期间的范围内,因此如果您想在外部使用它,请将其存储在变量中以便稍后重复使用,如下所示:

var currentUser = {};
var myRef = new Firebase("https://<your-firebase>.firebaseio.com");
var authClient = new FirebaseSimpleLogin(myRef, function(error, user) {
  if (error) {
    // an error occurred while attempting login
    console.log(error);
  } else if (user) {
    // user authenticated with Firebase
    currentUser = user;
  } else {
    // user is logged out
  }
});
...

// Later on in your code (that runs some time after that login callback fires)
console.log("User ID: " + currentUser.uid + ", Provider: " + currentUser.provider);
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/09/16/47f264558f4cbf68ac412056645ef24c.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告