如何让用户无论如何都使用Firebase登录?

时间:2017-03-28 15:14:00

标签: javascript angularjs firebase ionic-framework firebase-authentication

我正在根据Ionic Framework开发应用程序,我也在使用Firebase。

现在,经过几个小时或者应用程序崩溃或重新启动设备后,身份验证将丢失。

无论发生什么事情,我如何设法让我的用户始终登录?(例如Facebook)

以下是来自页面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()
}])

2 个答案:

答案 0 :(得分:2)

我会回答我自己的问题,因为我找到了解决方案:

就我而言,你必须使用这段代码:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    $state.go("tab.favorites");
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

答案 1 :(得分:0)

尝试在登录后将登录存储在sharedPreferences中或与外部资源一起存储,然后在用户注销时删除值。