Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear local storage after logout using angularjs

Tags:

angularjs

Html code

<div><a href="#/logout"><h2>Log Out</h2></a></div>

My routing

 app.config([ '$routeProvider', function($routeProvider) {

    $routeProvider.when('/', {
        templateUrl : 'app/components/login/Login.html',
        controller : 'loginCtrl'

    }).when('/register/', {
        templateUrl : 'app/components/register/register.html',
        controller : 'registerController'
    }).when('/welcome/', {
        templateUrl : 'app/components/welcome/welcome.html',
        controller : 'welcomeController'
    }).when('/home', {
        templateUrl : 'app/components/home/home.html',
        controller : 'homeController'
    }).when('/homeView', {
        templateUrl : 'app/components/home/homeView.html',
        controller : 'homeController'
    }).when('/graph', {
        templateUrl : 'app/components/home/graphView.html',
        controller : 'LineCtrl'
    }).when('/logout', {
        templateUrl : 'app/components/login/Login.html',
        controller : 'logoutCtrl'
    }).otherwise({
        redirectTo : "/"
    });
} ]);

Logout Controller

app.controller('LogoutController',function($location,$scope){
        $location.path('/');
});

It is working fine and after logout it goes to login page and if i press back button also it will not redirect to last page but i think this not the right way i am working and i am not clearing the local storage while logout,I stuck with how to clear the local storage values with logout, Please help me i am new to angularjs

login controller in which values are set to localstorage

 $scope.login = function () {
         $scope.empData=[]

         $scope.empData = loginService.get('http://183.1.1/HospitalManagementSystem/Service1.svc/LoginVerification/' + $scope.emailId + '/' + $scope.password);

         $scope.empData.then(function (empData) {
             console.log( $scope.empData)
             /*if (empData !== undefined && empData !== null){
                 if(empData._statusCode === constants.statusCode) {*/
                     if (empData.LoginVerificationResult.length === 0) {
                        console.log('Employee details are not Available');
                     } else {
                         $rootScope.name=empData.LoginVerificationResult[0].UserName;


                         localStorage.setItem("Role ID",empData.LoginVerificationResult[0].RoleID);
                         localStorage.setItem("UserId",empData.LoginVerificationResult[0].UserId);
                         localStorage.setItem("UserName",empData.LoginVerificationResult[0].UserName);                      
                                   $location.path('/welcome')
                     }
         });
     };

LoginService

app.service('loginService', [ '$http', function($http) {

    this.get = function(path) {
        var data = $http.get(path).then(function(response) {
            return response.data;
        });
        return data;
    };

} ]);
like image 658
Sudhir Avatar asked Dec 14 '15 09:12

Sudhir


1 Answers

I think you only need a localStorage.clear(); in the logout controller:

app.controller('LogoutController',function($location, $scope, $window){
    $window.localStorage.clear();
    $location.path('/');
});
like image 182
michelem Avatar answered Oct 19 '22 19:10

michelem