- 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.' });
}
}
推薦閱讀
- Visual C++程序設計學習筆記
- Java入門經典(第6版)
- Getting started with Google Guava
- Java系統分析與架構設計
- Java EE 8 Application Development
- R語言與網絡輿情處理
- INSTANT Yii 1.1 Application Development Starter
- Flowable流程引擎實戰
- Mastering SciPy
- C/C++代碼調試的藝術
- 菜鳥成長之路
- LibGDX Game Development By Example
- Spring MVC Blueprints
- Switching to Angular 2
- Continuous Integration,Delivery,and Deployment