Ui-Routes get the id of the logged in user

2

I'm using ANGULARJS / UI-ROUTES , and currently my application after login only keeps the email, I need to also have the user ID logged in. The issue of the routes I get, the problem is that he does not see the ID to be able to bring. I think the problem may be in my claims ?

code follows.

This is where I pass the id (which is null because "user" only comes the email)

<li ng-show="user"><a ui-sref="perfil({userId:user.id})">OLÁ, <em>{{ user }}</em></a></li>

Here is my controller that tries to get the id of the parameter that passes, but only comes null because the user.id is null

angular.module('scases').controller('PerfilCtrl', PerfilCtrl);

PerfilCtrl.$inject = ['$scope', 'UserFactory', '$stateParams'];

function PerfilCtrl($scope, UserFactory, $stateParams) {

    $scope.id = $stateParams.userId;

My real question is: Where will I get the user id logged in, if when I log in, just come the email?

    
asked by anonymous 26.05.2016 / 15:54

1 answer

0

Try using this ui-sref="perfil({userId: '{{user.id}}'})" . Check if this state waits for the userId parameter.

.state('perfil', {
    url: '/perfil',
    templateUrl: 'perfil.html',
    controller: 'Perfil',
    params: {
        userId: null
    }
})

To achieve the user's ID, you must create a service that returns the data you need. After obtaining this data, save it in the localstorage and create a service to make this data available to you.

Example:

angular.module('app').service('UsuarioService', UsuarioService);

UsuarioService.$inject = ['Restangular'];

function UsuarioService(Restangular) {
    var service = {
        getUsuario: function () {
            var promise = Restangular.one('usuarios').get();

            promise.then(function (response) {
                window.localStorage.setItem('usuario', JSON.stringify(response.data.plain()));
                return response.data;
            });

            return promise;
        },
        getUsuarioFromLocalStorage: function () {
            return JSON.parse(window.localStorage.getItem('usuario'));
        }
    };

    return service;
}
    
14.09.2016 / 17:35