San
San

Reputation: 347

How to get currently logged in user details from Promise object in jhipster based application

I am trying to create 'Order' object in the front end and pushing it into the database using REST services. POJO of 'Order' looks like below

@NotNull
@Field("total")
private BigDecimal total;

@Field("status")
private String status;

@Embedded
public User user;

Now I have a 'Principal' service which is providing information of the currently logged in user.I have tried 'console.log(Principal.identity())' which is returning result as shown below. Here 'User' data is present inside the '$$state' Object.
enter image description here I am not able to find out how to take 'user' data from promise object and add to the 'Order' object. I have dirty method to get user data by digging inside the Promise object as shown below but I am skeptical about this method. enter image description here

What is the correct way to get data from Promise in this scenario?

EDIT: This is jhipster based application. Below is the "Principle" service code


            'identity: function (force) {
            var deferred = $q.defer();

            if (force === true) {
                _identity = undefined;
            }

            // check and see if we have retrieved the identity data from the server.
            // if we have, reuse it by immediately resolving
            if (angular.isDefined(_identity)) {
                deferred.resolve(_identity);

                return deferred.promise;
            }

            // retrieve the identity data from the server, update the identity object, and then resolve.
            Account.get().$promise
                .then(function (account) {
                    _identity = account.data;
                    _authenticated = true;
                    deferred.resolve(_identity);
                    Tracker.connect();
                })
                .catch(function() {
                    _identity = null;
                    _authenticated = false;
                    deferred.resolve(_identity);
                });
            return deferred.promise;
        }'

Here is the jhipster generated method to receive resource from server using ngResource.

'angular.module('hotSpiceApp')
.factory('Order', function ($resource, DateUtils) {
    return $resource('api/orders/:id', {}, {
        'query': { method: 'GET', isArray: true},
        'get': {
            method: 'GET',
            transformResponse: function (data) {
                data = angular.fromJson(data);
                return data;
            }
        },
        'update': { method:'PUT' }
    });
});'

Upvotes: 0

Views: 3385

Answers (2)

Ruslan Stelmachenko
Ruslan Stelmachenko

Reputation: 5429

The Principal.identity() function returns a Promise. First read what is promise.

Then do something like:

Principal.identity().then(function (user) {
  var data = {
    // other fields
    user: user
  };
  // do something with data only inside this inner function
});

Doing Principal.identity().$$state.value is bad not only because it related on internal implementation of angular's promises but it also won't work in all cases. Promises by nature is asynchronous and this work only because Jhipster caches result of http request which returns current user. But if there are no info about current user at time of your new requst, then Principal.identity().$$state.value will be undefined because first it need to send http request to the server and only after that this promise will be "resolved" (internally it will set the value variable and call function defined in then method).

Also I should note, that you should not pass current user from JS code to the server. You should take current user on server side (from session or something) after request arrived and set it to your model if needed. Never trust user code (code which runs in user's browser). It can inject any value to this request and send it to the server.

Upvotes: 4

Ranjith V
Ranjith V

Reputation: 134

Can you explain the way you are retrieving the principal service? If it is returning a promise you need to set the data you want to a local variable in the "then" method.

Now to see what "Principal" service has , you can use the below code in chrome console

angular.element(document.querySelector('html')).injector().get(Principal);

Upvotes: 0

Related Questions