首页 > 解决方案 > DId firebase createUserWithEmailAndPassword() 是否更改了返回值?

问题描述

我已经使用这个代码一年了:

firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(user){
  console.log('uid',user.uid)

  //Here if you want you can sign in the user
}).catch(function(error) {
    //Handle error
});

但是,过去 2 天我抱怨新用户在创建新登录时遇到问题。我检查了这段代码中的“用户”变量,它看起来像这样:

{
  additionalUserInfo: {
    providerId: "password", 
    isNewUser: true 
  },
  credential: null,
  operationalType:"signIn"

  user: {
    (bunch of fields...)
    uid: "xxxxx123456789",
    (bunch of fileds...)
  }
}

这意味着我现在必须使用 user.user.uid 而不是 user.uid 来引用 uid。我做了这个更改并且代码可以工作,但是我还有这个函数的其他几个用途,我想知道这是否是一个实际的更改,或者我是否看到了一些不寻常的东西。我已经很久没有更改过这段代码了,所以无论如何我都很惊讶。欣赏任何见解。

标签: javascriptfirebaseauthenticationfirebase-authentication

解决方案


是的,它改变了许多方法,在我身边也崩溃了。但我必须重构所有这些。它返回一个UserCredential包含user和其他对象的类型。

UserCredential: {
   additionalUserInfo: { isNewUser: boolean, profile: Object, providerId: string, username: string  },
   credential: {accessToken: string, idToken: string, providerId: string, signInMethod: string},
   operationType: string,
   user: Object
}

这是我重构的示例代码,它也崩溃了signInWithPopup()方法。

firebase
   .auth()
   .signInWithPopup(provider)
   .then(authResult => {
        console.log({ authResult });

        if (authResult.user) {
            this.doLogin(authResult.user);
        }
   });

不幸的是,重构代码只是问题的解决方案。另请查看链接以获取 about createUserWithEmailAndPassword()


推荐阅读