获取Firebase上最近创建的用户的UID

获取Firebase上最近创建的用户的UID,firebase,firebase-security,firebasesimplelogin,Firebase,Firebase Security,Firebasesimplelogin,有没有办法获取最近创建的用户的UID 据我所知,它看起来并没有返回任何东西 我们将如何获取这些信息,以便开始存储有关用户的信息 我知道可以实现的一种方法是在创建时登录用户。但我不想覆盖现有会话 var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com'); firebaseRef.createUser({ email : "bobtony@firebase.com", password

有没有办法获取最近创建的用户的UID

据我所知,它看起来并没有返回任何东西

我们将如何获取这些信息,以便开始存储有关用户的信息

我知道可以实现的一种方法是在创建时登录用户。但我不想覆盖现有会话

var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com');
  firebaseRef.createUser({
    email    : "bobtony@firebase.com",
    password : "correcthorsebatterystaple"
  }, function(err) {
    if (err) {
      switch (err.code) {
        case 'EMAIL_TAKEN':
          // The new user account cannot be created because the email is already in use.
        case 'INVALID_EMAIL':
          // The specified email is not a valid email.
        case default:
      }
    } else {
      // User account created successfully!
    }
  });

创建用户后,您可以对其进行身份验证,如您链接到的页面上的代码示例上方所述:

使用指定的凭据创建新的基于电子邮件/密码的帐户。创建帐户后,可以使用authWithPassword()对用户进行身份验证


然后在
authWithPassword
回调中,您可以访问新用户的
auhtData

我在firebase的支持论坛上问了这个问题,雅各布给了我这个答案。我希望这能帮助任何有同样问题的人

复制粘贴自


您所需要做的只是对不同的Firebase上下文进行身份验证。创建新Firebase对象时,可以通过未记录的上下文参数来执行此操作

// adminRef will be used to authenticate as you admin user (note the "admin" context - also note that this can be ANY string)
var adminRef = new Firebase("https://<your-firebase>.firebaseio.com", "admin");
adminRef.authWithCustomToken("<token>", function(error, authData) {
  if (error !== null) {  
    // now you are "logged in" as an admin user

    // Let's create our user using our authenticated admin ref
    adminRef.createUser({
      email: <email>,
      password: <password>
    }, function(error) {
      if (error !== null) {
        // let's create a new Firebase ref with a different context ("createUser" context, although this can be ANY string)
        var createUserRef = new Firebase("https://<your-firebase>.firebaseio.com", "createUser");

        // and let's use that ref to authenticate and get the uid (note that our other ref will still be authenticated as an admin)
        createUserRef.authWithPassword({
          email: <email>,
          password: <password>
        }, function(error, authData) {
          if (error !== null) {
            // Here is the uid we are looking for
            var uid = authData.uid;
          }
        });
      }
    });
  }
});
//adminRef将用于作为管理员用户进行身份验证(请注意“admin”上下文-还请注意,它可以是任何字符串)
var adminRef=新Firebase(“https://.firebaseio.com“,”管理“);
adminRef.authWithCustomToken(“),函数(错误,authData){
如果(错误!==null){
//现在您已作为管理员用户“登录”
//让我们使用经过身份验证的admin ref创建用户
adminRef.createUser({
电邮:,
密码:
},函数(错误){
如果(错误!==null){
//让我们使用不同的上下文(“createUser”上下文,尽管它可以是任何字符串)创建一个新的Firebase ref
var createUserRef=新Firebase(“https://.firebaseio.com“,“createUser”);
//让我们使用该ref进行身份验证并获取uid(注意,我们的另一个ref仍将作为管理员进行身份验证)
createUserRef.authWithPassword({
电邮:,
密码:
},函数(错误,authData){
如果(错误!==null){
//这是我们正在寻找的uid
var uid=authData.uid;
}
});
}
});
}
});

请注意,我们不久将发布一个新版本的Firebase,它在createUser()回调中返回uid。在这一点上,就不需要这种有点粗糙的解决方法了。

Firebase最近发布了一个更新的JavaScript客户端(v2.0.5),它通过第二个参数直接向完成回调公开新创建用户的用户id。查看位于的更改日志,并查看以下示例:

ref.createUser({
  email: '...',
  password: '...'
}, function(err, user) {
  if (!err) {
    console.log('User created with id', user.uid);
  }
});

以上答案适用于旧firebase。 对于寻求新firebase实施的用户:

     firebase.auth().createUserWithEmailAndPassword(email, password)
      .then(function success(userData){
          var uid = userData.uid; // The UID of recently created user on firebase

          var displayName = userData.displayName;
          var email = userData.email;
          var emailVerified = userData.emailVerified;
          var photoURL = userData.photoURL;
          var isAnonymous = userData.isAnonymous;
          var providerData = userData.providerData;

      }).catch(function failure(error) {

          var errorCode = error.code;
          var errorMessage = error.message;
          console.log(errorCode + " " + errorMessage);

      });

来源:

当uidfirebase.auth()的console.log.onAuthStateChanged(函数(用户){,#这需要在signup@AnoobKBava是的!但这只是为了获取UID!我认为userData是更复杂的对象-要获取UID,需要使用userData.user.UID。