How to keep user logged in with Firebase no matter what?

早过忘川 提交于 2019-12-11 00:15:05

问题


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()
}])

回答1:


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.
  }
});



回答2:


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



来源:https://stackoverflow.com/questions/43073371/how-to-keep-user-logged-in-with-firebase-no-matter-what

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!