【问题标题】:Can't set user data after firebase auuthenticationfirebase 身份验证后无法设置用户数据
【发布时间】:2019-11-23 04:02:42
【问题描述】:

我正在 firebase 上开发一个 react 应用程序。火存储在火认证后没有设置userdata的问题,虽然它已经在谷歌上成功认证。如果你有一些解决这个问题的线索,请帮助我。而且我无法在此处收到错误文本。

  login = () => {
    const provider = new firebase.auth.GoogleAuthProvider();
    firebase
      .auth()
      .signInWithRedirect(provider)
      .then(result => {
        const user = result.user;
        const userRef = firebase
          .firestore()
          .collection("users")
          .doc(user.uid);

        userRef.get().then(function(doc) {
          if (doc.exists) {
            console.log("User data:", doc.data());
          } else {
            // doc.data() will be undefined in this case
            userRef.set({
              uid: user.uid,
              name: user.displayName,
              photoURL: user.photoURL
            });
          }
        });
      });

    this.props.history.push("/");
  };

附加

'user' is not defined.
'userRef' is not defined

我已经尝试了代码。但是这里不能定义 user 和 userRef:

    if (doc.exists) {
      console.log("Doc for user " + user.uid + " already exists");
      throw new Error("Doc for user " + user.uid + " already exists");
    } else {
      return userRef.set({
        uid: user.uid,
        name: user.displayName,
        photoURL: user.photoURL
      });
    }

附加2

我不知道为什么,但是 then 在 Firebase 身份验证上不起作用。

login = () => {
  let user;
  const provider = new firebase.auth.GoogleAuthProvider();
  firebase
    .auth()
    .signInWithRedirect(provider)
    .then(result => {
      console.log("result", result);
      // result has been skipped

【问题讨论】:

    标签: reactjs firebase google-cloud-firestore


    【解决方案1】:

    实际上,signInWithRedirect() 方法返回一个没有返回值的承诺(即Promise<void>)。因此做

        firebase
          .auth()
          .signInWithRedirect(provider)
          .then(result => {...});
    

    不会工作。

    您需要使用getRedirectResult() 方法,如文档中所述:

    使用整页重定向流程对 Firebase 客户端进行身份验证。到 处理此操作的结果和错误,请参阅 firebase.auth.Auth.getRedirectResult.

    因此,在您的 login 函数中,您应该只有两行:

      login = () => {
        const provider = new firebase.auth.GoogleAuthProvider();
        firebase
          .auth()
          .signInWithRedirect(provider);
      }
    

    在你的代码中的其他地方(我不知道确切的位置,因为我不知道 reactjs...)你需要有以下代码(注意不同的 Promise 是如何chained):

        let user;
    
        firebase
        .auth()
        .getRedirectResult()
        .then(result => {
            user = result.user;
            const userRef = firebase
              .firestore()
              .collection('users')
              .doc(user.uid);
    
            return userRef.get();
          })
          .then(doc => {
            if (doc.exists) {
              console.log('Doc for user ' + user.uid + ' already exists');
              throw new Error('Doc for user ' + user.uid + ' already exists');
            } else {
              return doc.ref.set({
                uid: user.uid,
                name: user.displayName,
                photoURL: user.photoURL
              });
            }
          })
          .then(() => {
            this.props.history.push('/');
          })
          .catch(error => {
            console.log(error);
            this.props.history.push('/errorPage');
          });
    

    请注意,如果多个用户能够使用同一个 Google 帐户登录,您可能需要在检查userRef 上的文档不存在时使用Transaction

    【讨论】:

    • 非常感谢@renaud-tarnec 但这行不通,因为获取 doc 后的代码没有 userRef 和用户数据。 return userRef.set({ uid: user.uid, name: user.displayName, photoURL: user.photoURL }); User 和 userRef 未定义。如何定义?
    • 你的意思是在then()之后userRef.set({...})你要使用user.data吗?还是user.uid?你能详细说明你到底想做什么吗?
    • 感谢我已添加到我的问题描述中。
    • 哦,我现在明白了!确实有一个错误,我没有测试过代码......我已经调整了答案,现在应该没问题了。告诉我它是否有效!
    • 谢谢我完全明白了!
    猜你喜欢
    • 2020-12-07
    • 2016-04-22
    • 2019-06-05
    • 2021-09-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    相关资源
    最近更新 更多