简体   繁体   English

Firebase / React:onAuthStateChanged()是在注册函数的时间之前还是之后触发的?

[英]Firebase/React: is onAuthStateChanged() triggered before or after the then of a sign up function?

okay, here is something really weird, guys. 好的,这真的很奇怪,伙计们。

I have an App class and a SignInWithPassword class. 我有一个App类和一个SignInWithPassword类。 SignInWithPassword gets rendered in App as long as no user is signed in. I have a form where users can enter their username of choice, email, and password and then sign up. 只要没有用户登录, SignInWithPassword就会在App呈现。我有一个表单,用户可以在其中输入他们选择的用户名,电子邮件和密码,然后进行注册。 I have a user object inside App's state which is set inside onAuthStateChanged . 我在App的状态内有一个user对象,该对象在onAuthStateChangedonAuthStateChanged I've been facing issues with setting the displayName of App.state.user.displayName because when you sign in with just an email and password, firebase's default user.displayName is set to null . 我一直在设置App.state.user.displayName的displayName时App.state.user.displayName问题,因为当您仅使用电子邮件和密码登录时, user.displayName的默认user.displayName设置为null I tried using updateProfile but that still doesn't set App 's user.displayName , it only update's firebase's user object, and updateProfile won't trigger onAuthStateChanged. 我尝试使用updateProfile但仍未设置Appuser.displayName ,它仅更新了user.displayNameuser对象,并且updateProfile不会触发onAuthStateChanged。 So then, i thought i would try creating a setDisplayName method inside App , pass it as a prop to SignInWithPassword , and call it inside my signUp's then . 因此,我想尝试在App内创建setDisplayName方法,将其作为道具传递给SignInWithPassword ,然后在signUp的then调用它。 but that STILL doesn't change App.user.displayName UNLESS I REFRESH IT. 但是除非我重新App.user.displayName STILL不会更改App.user.displayName Upon doing some logging, I noticed something strange. 在进行一些日志记录时,我发现了一些奇怪的东西。

I have 3 console.log() s. 我有3 console.log() One inside SignInWithPassword 's signUp method: SignInWithPassword内部的signUp方法之一:

signUp = () => {
    firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password).then(() => {
      console.log(firebase.auth().currentUser) //log 1
      firebase.auth().currentUser.updateProfile({
        displayName: this.state.username,
      })
      this.props.setUsername(this.state.username)
    }).catch((error) => {
      // Handle Errors here.
      alert(error.message)
      this.setState({
        email: '',
        password: '',
      })
    }) 
  }

one inside onAuthStateChanged in App , App onAuthStateChanged内部,

firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        console.log(user.displayName) //log 2
        const currentUser = {
          displayName: user.displayName,
          email: user.email,
          uid: user.uid,
        }
        this.setState({
          user: currentUser,
        })
      } else {
        this.setState({
          user: {}
        })
      }
    })
  }

and one inside App 's setUsername() method: AppsetUsername()方法中的一个:

setUsername = (username) => {
    const user = Object.assign({}, this.state.user)
    user.displayName = username
    this.setState({
      user,
    })
    console.log(this.state.user.displayName) //log 3
  }

the order in which the logs were displayed is log 1, log 3, log 2. this clearly means that onAuthStateChanged is only being called after the then stuff in signUp . 在曾显示日志的顺序是1日志,日志3,日志2.这显然意味着onAuthStateChanged只被后称为then在东西signUp which means that log 2 should print the username that the user entered in the form, right? 这意味着log 2应该打印用户在表单中输入的用户名,对吗? WRONG. 错误。 it prints null . 它输出null Can someone explain what's going on?? 有人可以解释发生了什么吗?

I had this problem a few hours ago, I changed 几个小时前我遇到了这个问题,我改变了

firebase.auth().currentUser.updateProfile({

with

 auth.doCreateUserWithEmailAndPassword(email, passwordOne)
    .then(authUser => {
    this.setState(() => ({ ...INITIAL_STATE }));
    authUser.user.updateProfile({
        displayName: firstName + " "+lastName
      })

it worked for me this way and not with .currentUser 它以这种方式为我工作,而不是与.currentUser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM