2

I am developing an app under the Ionic Framework and I am also using Firebase.

Now, it happens that after a few hours or for a crash of the app or restarting the device, the Authentication is lost.

How can I manage to have my user ALWAYS logged in, no matter what happen ? (Like Facebook for example)

Here is my Login Controller from the page login.html :

.controller('loginController',['$scope', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope, $firebaseArray, CONFIG, $document, $state) {
  // Perform the login action when the user submits the login form
  $scope.doLogin = function(userLogin) {
        if($document[0].getElementById("user_name").value != "" && $document[0].getElementById("user_pass").value != ""){
        firebase.auth().signInWithEmailAndPassword(userLogin.username, userLogin.password).then(function() {
          var userId = firebase.auth().currentUser.uid;
          firebase.database().ref('accounts/' + userId + '/currentBusiness/').update({
            name: "No current business arround",
            description: "Seems there's nothing arround...",

            })
                      $state.go("tab.favorites");

        }, function(error) {
          // An error happened.
          var errorCode = error.code;
          var errorMessage = error.message;

          if (errorCode === 'auth/invalid-email') {
             alert('Enter a valid email.');
             return false;
          }else if (errorCode === 'auth/wrong-password') {
             alert('Incorrect password.');
             return false;
          }else if (errorCode === 'auth/argument-error') {
             alert('Password must be string.');
             return false;
          }else if (errorCode === 'auth/user-not-found') {
             alert('No such user found.');
             return false;
          }else if (errorCode === 'auth/too-many-requests') {
             alert('Too many failed login attempts, please try after sometime.');
             return false;
          }else if (errorCode === 'auth/network-request-failed') {
             alert('Request timed out, please try again.');
             return false;
          }else {
             alert(errorMessage);
             return false;
          }
        });
    }else{
        alert('Please enter email and password');
        return false;
    }//end check client username password
  };// end $scope.doLogin()
}])
AL.
  • 36,815
  • 10
  • 142
  • 281
FrenchyNYC
  • 337
  • 1
  • 6
  • 23

2 Answers2

2

I will answer my own question because I found the solution :

In my case, you have to use this code :

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    $state.go("tab.favorites");
    // User is signed in.
  } else {
    // No user is signed in.
  }
});
FrenchyNYC
  • 337
  • 1
  • 6
  • 23
  • 1
    I don't understand how this answers the question. To answer the question, you'd need to show what goes in the else block so we can see how you actually get Firebase to reload the user object without having the user sign in again. – Javid Jamae Jul 08 '17 at 22:50
0

Try to store Login in a sharedPreferences or with an external resources once logged in, and then remove value when an user logs out.