使用ionic angularjs创建cookie,并将cookie持久化很长时间

Create cookie using ionic angularjs and persist the cookie to a long time

本文关键字:cookie 持久化 长时间 并将 angularjs 创建 使用 ionic      更新时间:2023-09-26

我在Ionic/angularjs框架中创建了一个登录系统。

我想存储一个cookie并设置过期时间很长,所以用户不需要每次登录,因为它是一个内部应用程序。

我只是添加http post到这个控制器,还不能处理cookie依赖和cookie创建:

.controller('AppCtrl', function($scope, $ionicModal, $timeout, $http, $location, $state ) { //, $cookieStore, Auth
  $scope.loginData = {};
  $scope.doLogin = function() {
    if(!angular.isDefined($scope.loginData.login)
        || !angular.isDefined($scope.loginData.password)
        || $scope.loginData.login.trim() == ""
        || $scope.loginData.password.trim() == ""){
       alert("Digite seu usuário e senha");
       return;
    }
    $http.post('http://www.somedomain.com/somefile.php',$scope.loginData)
      .then(
        function(result) {
          $scope.response = result;
          angular.forEach(result.data, function(value, key) {
            console.log(key + ': ' + value);
            if( value.slice(-1) == 1 ) {
              $location.path('/app/playlists');
            } else {
                $ionicModal.fromTemplate('<button class=button>try again</button>').show();
            }
          });
      });
  };
  $scope.logout = function() {
    Auth.logout();
    $state.go("acesso");
  };
})

使用angular-local-storage,您可以使用setStorageCookie,这里是文档链接:https://github.com/grevory/angular-local-storage