- 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.' });
}
}
推薦閱讀
- Python數據分析入門與實戰
- 羅克韋爾ControlLogix系統應用技術
- Python爬蟲開發與項目實戰
- 深入淺出PostgreSQL
- Node.js Design Patterns
- Building Microservices with .NET Core
- Mastering ArcGIS Enterprise Administration
- PrimeFaces Blueprints
- Learning Ionic
- Data Science Algorithms in a Week
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- Learn C Programming
- Practical Time Series Analysis
- Neo4j Graph Data Modeling
- 算法(第4版)