I need to store all the items in $localStorage
like this:
$localStorage.userData.currentUser = data.name;
$localStorage.userData.devId= data.id;
$localStorage.userData.userRole = data.roles[0].name;
$localStorage.userData.userId = data.user_id;
Here is the controller code:
myApp.controller('loginController', ['$rootScope', '$scope', 'Auth', '$state', '$localStorage',
function ($rootScope, $scope, Auth, $state, $localStorage) {
function successAuth(res) {
$localStorage.token = res;
$scope.currentUser = $localStorage.token.name;
console.log("res: ", res);
Auth.setUserData(res, function(){
$scope.error = 'No User data available';
});
if ($localStorage.token) {
$state.go("dashboard");
$scope.isLogin = true;
} else {
alert("Invalid username or password");
}
}
$scope.signin = function () {
var formData = {device_id: myip, platform: "WEB", provider: "EMAIL", uid: $scope.login.username, access_token: "", password: $scope.login.password};
Auth.signin(formData, successAuth, function () {
$rootScope.error = 'Invalid credentials';
console.log($rootScope.error);
});
};
$scope.isLogin = !angular.isUndefined($localStorage.token);
}]);
I attempted the following based on the documentation, but it didn't work. It's showing an error saying '$storage' is undefined:
$scope.$storage = $localStorage;
$storage.userData.currentUser = data.name;
$storage.userData.devId= data.id;
$storage.userData.userRole = data.roles[0].name;
$storage.userData.userId = data.user_id;
How can I properly save all the data into $localStorage
as mentioned above?