user6039980 user6039980 - 6 months ago 271
Javascript Question

Firebase: During testing I get "undefined is not a function"

This is my used code:

var cred = firebase.auth().EmailAuthProvider($scope.data.mail, $scope.data.pwd);

firebase.auth().signInWithCredential(cred)
.then(function()


During testing, I get the following error:


TypeError: undefined is not a function (evaluating
'firebase.auth().EmailAuthProvider($scope.data.mail,
$scope.data.pwd)')


So what's wrong with my code? How can I fix this problem?

Answer

To log a user in via email/pass you just call the signInWithEmailAndPassword method.

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

Then you can listen for the realtime auth event:

firebase.auth().onAuthStateChanged(function(user) {
  if (user !== null) {
    console.log('logged in!');
  }
});

Check the docs here for more on email login.

Check the API reference for onAuthStateChanged for more info.