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

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.' });
}
}
主站蜘蛛池模板: 瓦房店市| 忻城县| 安丘市| 阳东县| 南丹县| 嘉善县| 望奎县| 和龙市| 澳门| 榆林市| 榆树市| 宝坻区| 镇康县| 淮北市| 墨江| 苗栗市| 开远市| 搜索| 呈贡县| 视频| 柳江县| 交城县| 灵川县| 碌曲县| 奉化市| 邻水| 太白县| 武清区| 桃园市| 寻甸| 扶沟县| 莎车县| 许昌县| 永顺县| 云安县| 周口市| 峨眉山市| 合水县| 青海省| 建宁县| 哈尔滨市|