Reputation: 11
I have a button that does login. If I click the button it will link to the home page(I used href). But I would like to check if the email and password are the correct information through the firebase.auth.signin(). I tried to use the event.preventDefault() but it doesn't work if I wait until firebase.auth reply and the page will just go to the homepage no matter what. How could I wait until firebase.auth finish work and do the preventDefault correctly?
more info) I tried async but it didn't work. I did something like below. It seems like preventDefault() is not working properly because of some time issue. It seems like if href doesn't receive answer in few sec, it just jumps into the next page before fire.auth gives back the message :(
async c (e) {
try {
if(this.state.email == null || this.state.password == null){
e.preventDefault();
}
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: ".",
messagingSenderId: ""
};
!firebase.apps.length ? firebase.initializeApp(config) : firebase.app();
await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
.then(function(user){
},
function(error) {
e.preventDefault();
});
} catch (error) {
e.preventDefault();
}
} <a href="/mainpage"><a>
Upvotes: 0
Views: 1051
Reputation:
You can use async/await for the purpose..
https://codeburst.io/async-await-into-react-79f154ea84b7
Upvotes: 4