Ya Wang
Ya Wang

Reputation: 1808

AngularJS $rootScope.authenticated resets?

It seems like when I redirect from one page to my mainpage my rootscope resets?

Here is the redirection code after login:

self.authUser = function() {
                LoginService.authUser($scope.inputEmail, $scope.inputPassword, 'inf')
                        .then(function(d) {
                            $rootScope.authenticated = true;
                            $window.location.href = '/job';
                        }, function(errResponse) {
                            $scope.errorLogin = true;
                            $rootScope.authenticated = false;
                        });
            };

Here is the code for the mainpage:

    <script src="<c:url value="/resources/js/application.js"/>"></script>
    <script src="<c:url value="/resources/js/service/main_service.js"/>"></script>
    <script
        src="<c:url value="/resources/js/controller/main_controller.js"/>"></script>
<title>Insert title here</title>
</head>
<body ng-app='application'>
<div class='container' ng-controller='MainController as ctrl'>
    <span ng-show='authenticated'>lllllllllllllllloloooooo</span>
    <button class="btn btn-lg btn-primary btn-block" ng-click='ctrl.authorize()'>Sign
            in</button>
            </div>
</body>

So after a successful login I set the rootScope to true, but then when it does $window.location.href it reset the true and its a false again. Do I need to reauthenticate every page via a post response on load or something?

Edit: ngRoute

application.js

'use strict';

var App = angular.module('application', [ 'ngRoute', 'ngCookies' ]).config(
        [ '$routeProvider', function($routeProvider) {
            $routeProvider.when('/', {
                templateUrl : '/job/',
                controller : 'main_controller'
            }).when('/login', {
                templateUrl : '/job/login',
                controller : 'login_controller'
            }).otherwise({
                redirectTo : '/'
            });
        } ]);

Tried $location.path("/"); $scope.$apply();

and $window.location.assign('/job');

also

$timeout(function () {
                            $location.path("/");
                        });

$location doesn't change page & $window refreshes my rootscope

Upvotes: 0

Views: 149

Answers (1)

Ya Wang
Ya Wang

Reputation: 1808

I wasn't actually using rootscope properly and nGroute was actually not being used properly.

Upvotes: 0

Related Questions