Error: loginService is undefined

Hello !

I have a problem when i try to kill the session withe the logout button.

The console show me : Error: loginService is undefined.

Here’s the code :

DashboardCtrl.js :

'use strict';

app.controller('dashboardCtrl', ['$scope', function($scope,loginService){
    $scope.txt= 'Dashboard Panel';
    $scope.logout=function(){
            loginService.logout();           
        }
}]);

loginService.js :

'use strict';

app.factory('loginService',function($http,loginService,$location){
    return{
        login:function(user,scope){
            return $http.post('data/user.php',user); //send data to user.php
        },
        logout:function(){
            sessionService.destroy('user');
            $location.path('/login');
        }
    }   
});

SessionService.js :

'use strict';

app.factory('sessionService', ['$http', function($http){
    return{
        set:function(key,value){
            return sessionStorage.setItem(key,value);
        },
        get:function(key){
            return sessionStorage.getItem(key);
        },
        destroy:function(key){
            $http.post('data/destroy_session.php');
            return sessionStorage.removeItem(key);
        }
    };
}])

Hi. Do you add loginService.js to yout index.html?

If you did it, fix parameters for your ‘loginService’ factory.

app.factory('loginService',function($http,loginService,$location)

Should be:

app.factory('loginService',function($http,sessionService,$location)

Then add loginService to your controller:

app.controller('dashboardCtrl', ['$scope', 'loginService', function($scope,loginService)
2 Likes

Thank you, you helped me a lot :wink: