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

时间:2014-11-20 17:29:43

标签: firebase firebase-security firebasesimplelogin

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

根据createUser() documentation,它看起来不会返回任何内容。

如何获取此信息以便我们可以开始存储有关用户的信息?

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

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

4 个答案:

答案 0 :(得分:4)

以上答案适用于旧火场。 对于那些寻找新的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);

      });

来源:Firebase Authentication Documentation

答案 1 :(得分:2)

Firebase最近发布了一个更新的JavaScript客户端(v2.0.5),它直接通过完成回调的第二个参数公开新创建用户的用户ID。查看https://www.firebase.com/docs/web/changelog.html的更改日志,并参阅下面的示例:

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

答案 2 :(得分:1)

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

  

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

然后在authWithPassword回调中,您可以访问新用户的auhtDatahttps://www.firebase.com/docs/web/api/firebase/authwithpassword.html

答案 3 :(得分:0)

我在firebase的支持论坛上问了这个问题,得到了Jacob的回答。我希望这有助于任何人遇到同样的问题。

http://groups.google.com/group/firebase-talk/

复制并粘贴

您只需要对其他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;
          }
        });
      }
    });
  }
});

请注意,我们很快会发布一个新版本的Firebase,它会在createUser()回调中返回uid。那时,不需要这种有点hacky的解决方法。