can't inject ngStorage into my controller

56 views Asked by At

hi i'm trying to inject ngtorage to handle sessions locally, i tried different ways to do it but without results:

here is my app.js

     var myApp = angular.module('myApp', ['ngStorage']);

loginController.js in a separated file:

    myApp.controller('logincontroller', ['$scope', '$http','log', function($scope, $http ,log,$sessionStorage)
 { 

$scope.auth = function(){ 

  log.logIn($scope.pseudo,$scope.pwd).
then(function(data){
 if (data.data.log == true){
    $scope.logged = true;
    $scope.success = "Hey ! t'es connect";
    $sessionStorage.user = data.data.pseudo;
 }else 
 {
    $scope.erreur = "connexion echouée";
    $scope.error = true;
 }

 }); 
 }
 }]);

and authSerivce.js the service that i'm injecting into my controller

     angular.module('myApp').factory('log', ['$http', function($http){
return{
    /*Check whether the user is logged in
    * @returns boolean
    */
   isLoggedIn: function isLoggedIn(){
     return session.getUser() !== null;
    },


/*Log in
* @param credentials
* @returns {*|Promise}
*/
logIn: function(username, pwd){
    return $http
     .post('/login', {'username':
    username, 'pwd': pwd})
     .success(function(data){
      if (data.log == true){
          console.log(data);
          //$localstorage.user= data.pseudo;
          return data;
      }

     }).error(function(data){
        return data;
     });
     },


/* Log out : on peut se poser la question d'une requête HTTP ! pour faire le
ménage côté serveur ?!
* @returns {*|Promise}
*/

 logOut: function(){
    return $http
    .get('/logout')
    .then(function(response){
    // Destroy session in the browser
    session.destroy();
    return(response.data);
    });
    }
}
  }]);

the line returning error is

  $sessionStorage.user = data.data.pseudo;

the error is Cannot set property 'user' of undefined

help much appreciated guys

1

There are 1 answers

0
Sajeetharan On BEST ANSWER

You are missing $sessionStorage in the order of parameters , change it as follows

myApp.controller('logincontroller', ['$scope', '$http','log','$sessionStorage',
  function($scope, $http ,log,$sessionStorage)