官术网_书友最值得收藏!

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.' });
}
}
主站蜘蛛池模板: 宜州市| 藁城市| 眉山市| 靖州| 南乐县| 南充市| 英超| 图片| 土默特左旗| 奉化市| 凤冈县| 新蔡县| 梅河口市| 新巴尔虎左旗| 云南省| 红桥区| 鄂尔多斯市| 漳浦县| 濮阳市| 安乡县| 休宁县| 孝感市| 宁河县| 宁陕县| 独山县| 新宁县| 平阳县| 千阳县| 六盘水市| 临邑县| 富阳市| 阜康市| 德保县| 平阳县| 丹巴县| 景德镇市| 繁峙县| 日土县| 右玉县| 东源县| 兴海县|