Upon successful login, I save the user's data in the localStorage and redirect them to the Dashboard. LoginCtrl:
(function() {
'use strict';
angular.module('BlurAdmin.pages.login')
.controller('LoginCtrl', LoginCtrl);
/** @ngInject */
function LoginCtrl($scope, $timeout, $http, $location, toastr) {
$scope.login = function() {
var data = { email: $scope.email, senha: $scope.password }
$http.post('http://xxxxxxx/snaapp/admin/login', data).
then(function(response) {
localStorage.token = response.data.token;
$http.get('http://xxxxxxx/snaapp/auth/user/info', { headers: { 'Authorization': response.data.token } }).
then(function(response) {
//Set values in localStorage
localStorage.user = JSON.stringify(response.data);
$location.path("/dashboard");
}).catch(function(fallback) {
toastr.error('Error logging in');
});
}).catch(function(fallback) {
toastr.error('Error logging in');
});
};
}
})();
How can I access data stored in the localStorage within a specific module?
(function() {
'use strict';
angular.module('BlurAdmin.pages.juridico', [
'BlurAdmin.pages.juridico.acoesColetivas'
])
.config(routeConfig);
/** @ngInject */
function routeConfig($stateProvider) {
//I would like to do something similar to this:
console.log(localStorage.user)
$stateProvider
.state('juridico', {
url: '/juridico',
template: '<ui-view autoscroll="true" autoscroll-body-top></ui-view>',
abstract: true,
title: 'Jurídico',
sidebarMeta: {
icon: 'ion-gear-a',
order: 100,
},
});
}
})();
The above code works only when the page is reloaded, but I need to access this data in the module without reloading the page.