- Progressive Web Apps with React
- Scott Domes
- 201字
- 2021-07-08 09:36:24
Back to authentication
Since we expect an error to be returned (since we haven't signed up with any email and password combination), we can leave our then statement blank, but add a console log to our catch statement:
handleSubmit = (event) => {
event.preventDefault();
this.setState({ error: '' });
if (this.state.email && this.state.password) {
firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
.then(res => { console.log(res); })
.catch(err => { console.log(err); })
} else {
this.setState({ error: 'Please fill in both fields.' });
}
}
Submit your form, and you should be returned the following error:
{code: "auth/user-not-found", message: "There is no user record corresponding to this identifier. The user may have been deleted."}
Great! This is exactly the error we wanted. This is the code we'll check for, before initiating the signup process. For now, we'll assume that all the other errors are due to an incorrect password:
handleSubmit = (event) => {
event.preventDefault();
this.setState({ error: '' });
if (this.state.email && this.state.password) {
firebase.auth().signInWithEmailAndPassword(this.state.email,
this.state.password)
.then(res => { console.log(res); })
.catch(err => {
if (error.code === 'auth/user-not-found') {
// Sign up here.
} else {
this.setState({ error: 'Error logging in.' }) ;
}
})
} else {
this.setState({ error: 'Please fill in both fields.' });
}
}
推薦閱讀
- SQL Server 從入門到項目實踐(超值版)
- Learning ROS for Robotics Programming(Second Edition)
- Manga Studio Ex 5 Cookbook
- Developing Middleware in Java EE 8
- 你不知道的JavaScript(中卷)
- SQL基礎(chǔ)教程(視頻教學(xué)版)
- Building Minecraft Server Modifications
- Python算法從菜鳥到達(dá)人
- BIM概論及Revit精講
- The DevOps 2.5 Toolkit
- HTML5秘籍(第2版)
- Advanced Express Web Application Development
- Android Studio Cookbook
- SwiftUI極簡開發(fā)
- Hacking Android