[英]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
对象,该对象在onAuthStateChanged
内onAuthStateChanged
。 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
但仍未设置App
的user.displayName
,它仅更新了user.displayName
的user
对象,并且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: 在
App
的setUsername()
方法中的一个:
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.