Javascript Firebase. Auth(). signInWithEmailAndPassword Not Be Able to Get Success Event
I Am Working with Java-Script Fire-Base in My App. I Am Able to createUserWithEmailAndPassword Successfully in App. Also It Is Showing That Data Inside...
I am working with java-script fire-base in my app.
I am able to createUserWithEmailAndPassword successfully in app. Also it is showing that data inside Fire-base Console.
Here is snippet.
firebase.auth().createUserWithEmailAndPassword("", "*******").catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log("Eroor Msg" + errorMessage);
// ...
});
Then I have gone through firebase.auth().signInWithEmailAndPassword.
With above method I am not be able to get any success event or message to log. Even it is not throwing any error.
Here is snippet.
firebase.auth().signInWithEmailAndPassword("", "*****")
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
alert(errorMessage);
}
console.log(error);
});
My question is how to get success event in this method signInWithEmailAndPassword ?
Any help will be greatly appreciated.
3 Answers
Just check the below code:
firebase.auth().signInWithEmailAndPassword("", "******")
.then(function(firebaseUser) {
// Success
})
.catch(function(error) {
// Error Handling
});
This must be done using onAuthState object, Below is the code to check if the user logged-in succesfully or not
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
});
you can run this code after you successfully signed in, BTW there is no object return by SignInWithEmailAndPassword to validate.
Can I be a little critical of other code I see to make people understand when to use "function" with a return event...
//run one function only
.then(function(firebaseUser) => this.myfunction(firebaseUser); );
//run lines of code
.then((firebaseUser) => {
// line1
// line2
);
My code for Ionic, might be a bit different for AngularCLI
public async dologinUser(newEmail: string, newPassword: string): Promise<any> {
try {
return await this._afAuth.auth.signInWithEmailAndPassword(newEmail, newPassword)
.then((firebaseUser) => {
if (firebaseUser) {
//do something
}
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage);
});
} catch (e) {
console.error("big hu?", e);
}
}