Ui-Routes get logged in user id

Asked

Viewed 963 times

3

I’m using ANGULARJS/UI-ROUTES, and currently my application after logging only gets the email, I need to also come the ID of the logged-in user. The question of the routes I can, the problem is that it does not see the ID to bring. I think the problem may be in mine Claims?

follows code.

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

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

Here is my controller that I try 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 logged in user id, if when I soon, only comes the email?

  • Preserve the user in a service or Factory, and from your controllers check if the service has any registered user or not.

1 answer

1

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 get the user id, you must create a service that returns the data you need. After obtaining this data, store it in 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.