【问题标题】:get UID of recently created user on Firebase获取最近在 Firebase 上创建的用户的 UID
【发布时间】:2015-01-18 16:24:13
【问题描述】:

有没有办法获取最近创建的用户的 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!
    }
  });

【问题讨论】:

    标签: firebase firebase-security firebasesimplelogin


    【解决方案1】:

    以上答案适用于旧火力基地。 对于那些正在寻找新的 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

    【讨论】:

    • 这才是真正的答案
    • "undefined" 当 console.log 为 uid 时
    • firebase.auth().onAuthStateChanged(function(user) {, #this需要注册后添加
    • @AnoobKBava 是啊!但这只是为了获取 UID!
    • 我认为 userData 是更复杂的对象 - 要获取 UID,您需要使用 userData.user.uid。
    【解决方案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);
      }
    });
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

        我在 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。到那时,就不需要这种有点老套的解决方法了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-30
          • 2020-11-30
          • 2017-12-20
          • 2017-12-14
          • 2017-09-04
          相关资源
          最近更新 更多