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

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.' });
}
}
主站蜘蛛池模板: 阿拉善左旗| 和平区| 鞍山市| 虹口区| 清涧县| 满洲里市| 井研县| 共和县| 右玉县| 新巴尔虎右旗| 内乡县| 恩施市| 济南市| 南丰县| 夏邑县| 夏津县| 安徽省| 通山县| 韶山市| 泰和县| 阳江市| 湘潭县| 南汇区| 和政县| 荣昌县| 明光市| 广宗县| 黄冈市| 黎城县| 蒲江县| 北川| 衡南县| 江安县| 东台市| 会理县| 武乡县| 高青县| 长武县| 沙河市| 三明市| 那坡县|