0

After clicking on logout button the related controller does not call so logout activity never happens.

I have written a login page using AngularJs and Spring boot and Security which is working fine in case of login but logout is not working.

I have just pasted the related parts of the files and pages.

index.html

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div ng-show="authenticated">
            <a href="#/"
               class="btn btn-info navbar-btn"
               role="button">
                Home
            </a>
            <a href="#/register-new-user"
               class="btn btn-info navbar-btn"
               role="button">
                Register New User
            </a>
            <a href="#/list-all-users"
               class="btn btn-info"
               role="button">
                List All Users
            </a>
            **<a href="#/logout"
               class="btn btn-danger navbar-btn pull-right"
               role="button">Logout
            </a>**
        </div>
    </div>
</nav>

app.js to rout the written URL

.when('/login',{
    templateUrl : '/login/login.html',
    controller : 'loginController'
})

**.when('/logout',{
    templateUrl : '/login/login.html',
    controller : 'logoutController'
})**

.otherwise({
    redirectTo : '/login'
});

controller.js to post the request to spring security.

app.controller('logoutController', function($rootScope, $scope, $http, $location, $route){

    console.log("inside of logout controller...");

    $scope.logout = function() {
        $http.post('logout', {}).finally(function() {

            $rootScope.authenticated = false;
            $location.path("/");

        });    
    }
});
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Peter
  • 21
  • 3
  • 12

1 Answers1

0

Remove the enclosing function, so that the POST request is immediately executed when the router instantiates the controller:

app.controller('logoutController', function($rootScope, $scope, $http, $location, $route){

    console.log("inside of logout controller...");

    ̶$̶s̶c̶o̶p̶e̶.̶l̶o̶g̶o̶u̶t̶ ̶=̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶)̶ ̶{̶
        $http.post('logout', {}).finally(function() {

            $rootScope.authenticated = false;
            $location.path("/");

        });    
    ̶}̶
});
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • removing the line doesn't help as it never reach to the logoutController , that's why I have put the the console.log to see if it comes inside or not. – Peter Jul 12 '19 at 11:34
  • Do the `List All Users` and `Register New Users` buttons work? – georgeawg Jul 12 '19 at 11:38
  • no, they don't as I haven't written their controller yet , I have only homeController,loginController and logoutController which are working fine except logoutController... isn't it related to the way which I have called them? – Peter Jul 12 '19 at 11:44
  • editing href based on https://stackoverflow.com/questions/41211875/angularjs-1-6-0-latest-now-routes-not-working from to SOLVED the issue...thank you. – Peter Jul 12 '19 at 12:10