bafix2203
bafix2203

Reputation: 541

Cookies with routing angularjs

I'm doing login to my site with angularjs. My code:
HTML:

        <input type="text" ng-model="username" placeholder="Username">

        <input type="password" ng-model="password" placeholder="Password" >

        <button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="signin()">Sign in</button> 

JS:

app.config(function($routeProvider) {
        $routeProvider
            .when('/', {
                 resolve: {
                    "check": function($location, $rootScope) {
                        if(!$rootScope.valueCookie) {
                            $location.path('/login');
                        }
                    }
                },
                templateUrl: 'home.html'
            })
            .when('/login', {
                templateUrl: 'login.html'
            })
            .otherwise({
                redirectTo: '/'
            });
    });

 $scope.signin = function() {
        $rootScope.valueCookie = $cookieStore.put('obj', someSessionObj);
    }

What I try to do is cookies resolve. When cookies are in a browser - after click sign in, and we are in "home.html", after refresh page there's still "home.html". My code doesn't work. Thanks for answers in advance.

Upvotes: 0

Views: 44

Answers (1)

Akashii
Akashii

Reputation: 2281

You just set cookies for $rootScope.valueCookie , you should get it and check it exists or not . Change your code to

 $cookies.put('obj', someSessionObj); 
 $rootScope.valueCookie = $cookies.get('obj');

from the Doc : $cookieStore is deprecated: (since v1.4.0)

Upvotes: 1

Related Questions