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

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.' });
}
}
主站蜘蛛池模板: 泰和县| 镇沅| 宁强县| 通州区| 庄河市| 探索| 彭阳县| 咸宁市| 梨树县| 濮阳县| 明光市| 南木林县| 奉节县| 海安县| 巩留县| 怀化市| 新蔡县| 西贡区| 高唐县| 和硕县| 常熟市| 定日县| 兖州市| 维西| 玉环县| 隆昌县| 赞皇县| 福安市| 咸阳市| 安多县| 靖远县| 北京市| 桃江县| 茶陵县| 常州市| 马边| 铁岭县| 潜山县| 临海市| 六安市| 北川|