I have created a 'register' component in my React web app which successfully registers users upon submission.
During that flow, I am attempting to set the 'displayName' too with a '.then' call.
This for whatever reason though isn't working. I'm attempting to follow the documentation, but no luck unfortunately.
Would somebody be able to point out what I'm doing wrong?
Here's my component:
class Register extends Component {
constructor(props) {
super(props)
this.state = {
displayName: "",
email: "",
password: ""
}
}
onChange = (e) => {
this.setState({ [e.target.name]: e.target.value})
}
onSubmit = async (e) => {
e.preventDefault()
await firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
.then(function(result) {
console.log(result);
console.log("Successfully created new user.")
return result.user.updateProfile({
displayName: this.state.displayName
})
})
.catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
console.log(errorMessage);
});
this.props.history.push("/dashboard")
}
render() {
return (
<section className="register-section">
<form className="register-form" onSubmit={this.onSubmit}>
<label className="register-label">Full Name</label>
<input className="register-input" type="text" name="displayName" onChange={this.onChange} value={this.state.displayName} />
<label className="register-label">Email</label>
<input className="register-input" type="email" name="email" onChange={this.onChange} value={this.state.email} />
<label className="register-label">Password</label>
<input className="register-input" type="password" name="password" onChange={this.onChange} value={this.state.password} />
<button className="register-button" type="submit" name="button">Continue</button>
<a className="register-link" href="sign-in">Got an account already?</a>
</form>
</div>
</section>
)
}
}
export default Register;
Any help would be appreciated!
EDIT: Here's the solved code.
onSubmit = async(e) => {
e.preventDefault()
await firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
.then(() => {
console.log("Successfully created new user.")
console.log(this.state.displayName)
let firebaseUser = firebase.auth().currentUser;
return firebaseUser.updateProfile({
displayName: this.state.displayName
})
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
console.log(errorMessage);
});
this.props.history.push("/dashboard")
}