- 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.' });
}
}
推薦閱讀
- Mastering Entity Framework Core 2.0
- Advanced Machine Learning with Python
- C++ Builder 6.0下OpenGL編程技術(shù)
- 信息可視化的藝術(shù):信息可視化在英國(guó)
- Clojure for Domain:specific Languages
- Web全棧工程師的自我修養(yǎng)
- Access 2016數(shù)據(jù)庫(kù)管
- Java 11 Cookbook
- 劍指大數(shù)據(jù):企業(yè)級(jí)數(shù)據(jù)倉(cāng)庫(kù)項(xiàng)目實(shí)戰(zhàn)(在線教育版)
- ASP.NET開(kāi)發(fā)與應(yīng)用教程
- SSM開(kāi)發(fā)實(shí)戰(zhàn)教程(Spring+Spring MVC+MyBatis)
- Python從入門(mén)到精通
- 移動(dòng)互聯(lián)網(wǎng)軟件開(kāi)發(fā)實(shí)驗(yàn)指導(dǎo)
- 跟戴銘學(xué)iOS編程:理順核心知識(shí)點(diǎn)
- Java高級(jí)程序設(shè)計(jì)