1

I'me a noob on AngularJS. I'm trying to manage cookies but it doesn't works as I want. I've two version of this code that seams identical but if the first one works... The second one failed.

The code for my login form is here :

(function() {
    var app = angular.module('userApp', ['suiviChantiers', 'ngCookies']);

    app.controller('UserLogin', ['$scope', '$http', '$cookies',
        function($scope, $http, $cookies) {

          $scope.login = function() {
            $scope.user = this.user;

            $http.post('services/user.php', {
              action: 'login',
              user: $scope.user
            }).success(function(data) {
              if (data.error) {
                // [...]
              } else if (data.message == 'success') {
                $cookies.user = data.data;
                console.log("User : " + $cookies.user); // Output is : User : MS1OVFEwWkRZeU5ETTBNVFV3TVE9PQ==
                window.setTimeout(function() {
                  if ($cookies.redirectTo == null) {
                    document.location.href = "chantier/index.html";
                  } else {
                    var dest = $cookies.redirectTo;
                    $cookies.redirectTo = null;
                    document.location.href = dest;
                  }
                }, 3000);
              }
            });
          };
          // [...]
        })();
    })();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-cookies.min.js"></script>
<h1>Login form ...</h1>

And in my "chantier/index.html" controller I have this :

(function() {
	var app = angular.module('chantierApp', ['ngCookies']);
	
	app.controller('ChantierListController', ['$scope', '$cookies', 'userService', function($scope, $cookies, userService) {
		$scope.user = userService.user;
		console.log("User : "+$cookies.user); // Output is : User : undefined but $cookies.redirectTo is not empty

	}]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-cookies.min.js"></script>

(Sorry for this so long code post) Can you explain me why cookies are not updated?

Thanks for help.

1 Answer 1

1

Finaly I solved my problem ... Everything was ok. But there was some very very persistent cache on some files (even after deleting cache files and tmp files). I launched my browser in private mode and everything worked.

Thanks for your help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.