0

I'm using below function to login an existing user with firebase authentication, but for some reason nothing happens. I don't get an error or confirmation. I can paste the same code into my DOM console and get logged in, but for some reason nothing when I call this function from my JS file. I've confirmed that the input's for email/password are right, and believe this may be an Async issue, but not sure how to go upon resolving it.

 $('.user-login').on("submit", function () {
   var email = $('.email-login').val().trim();
   var password = $('.password-login').val().trim();
   console.log(email + ' ' + password)
firebase.auth().signInWithEmailAndPassword(email, password)
  .then(function () {
    console.log('logged in')
  })
  .catch(function (error) {
    console.log(error);
  });

})

9
  • If the same code works in another context, clearly the problem is not (just) in this code. Please be sure to give us enough context to reproduce the problem, for example how you use this code in the JS file you mention. For more on this see, how to create a minimal, complete, verifiable example. Commented Dec 10, 2018 at 3:38
  • thanks for the quick reply Frank. I have a form/button on my index page with class="user-login". I've used debugger to ensure I'm going into this function when I click the form button, and using the console logs I made sure the user/pass are correct. Hope this helps, please let me know if I where I can add more context... Commented Dec 10, 2018 at 3:58
  • OK, so it looks like you call signInWithEmailAndPassword when the user submits the email+password form. What's the problem after that? Does either of the console.log() statements get printed? Commented Dec 10, 2018 at 4:13
  • no sir, neither of them get printed Commented Dec 10, 2018 at 4:20
  • Interesting. I don't immediately see what's wrong with the code. Do you have somewhere, where I can see the problem in action? Commented Dec 10, 2018 at 4:33

2 Answers 2

1

.onAuthStateChanged is how to handle the signin.

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks Ron! yes, I am using above mentioned function to capture state changes.
0

I am using this code from a while and its working perfectly fine on all browsers. and this will also provide you with errors in case they occur.

function signIn() {
  var email = document.querySelector("#email").value;
  var password = document.querySelector("#password").value;
  firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    window.alert(errorMessage);
  });
}

and this to handle events on user state change like he login and logout.

var user = firebase.auth().currentUser;

    firebase.auth().onAuthStateChanged(function (user) {
          if (user) {

        }
        });

1 Comment

thanks for your response Muhammad. Unfortunately for me, the code that I'm using is identical to what you've posted and still having the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.