How to get the current registered user using the Principal or Account service

I am trying to download $ scope.projects specific to registered users. REST side api

@RequestMapping(value = "/users/{id}/projects", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<List<Project>> getAllProjects(@PathVariable("id") String id,
        HttpServletResponse response)
        throws URISyntaxException {
    log.debug("REST request to get User : {}", id);
    User user = userRepository.findOneByLogin(id);
    if (user == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
    List<Project> page = projectRepository.findByUserIsCurrentUser();
    return new ResponseEntity<List<Project>>(page,
            HttpStatus.OK);
}

Checked this with a swagger client.

    angular.module('tfmappApp')
.factory('UserProject', function ($resource) {
    return $resource('api/users/:id/projects', {}, {
            'query': {method: 'GET', isArray: true, params: { id: '@login'}},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    return data;
                }
            }
        });
    });

angular.module('tfmappApp')
.controller('ProjectController', function ($scope, Principal, Project, User, UserProject, ParseLinks) {
    $scope.projects = [];
    $scope.page = 1;

    Principal.identity().then(function(account) {
        $scope.account = account;
        $scope.isAuthenticated = Principal.isAuthenticated;
    });
    $scope.loadAll = function() {
        $scope.projects = UserProject.query({id: $scope.account.login});

    };
    $scope.loadPage = function(page) {
        $scope.page = page;
        $scope.loadAll();
    };
    $scope.loadAll();});

The main service is Jhipster. The code below does not work, or I'm missing something.

Principal.identity().then(function(account) {
        $scope.account = account;
        $scope.isAuthenticated = Principal.isAuthenticated;
    });

How can I get the current user?

What is the correct way to get an account or principal or user who is currently registered?

I am using HTTP session authentication with Jhipster.

+4
source share
2 answers

I have a solution to this problem. Not what I want.

@Query("select project from Project project where project.user.login = ?#{principal.username}")
List<Project> findByUserIsCurrentUser();

- , jhipster, . - .

, URL REST, /users/{id}/projects, /projects factory, java rest controller.

.

-.

+1

, , ... , . , . , Principal, , ... ( , ):

var principalService = this;
// retrieve the identity data from the server, update the identity object, and then resolve.
Account.get().$promise
            .then(function (account) {
                _identity = account.data;
                principalService.currentIdentity = angular.copy(_identity);
                console.log('current identity :' + JSON.stringify(principalService.currentIdentity));

                _authenticated = true;
                deferred.resolve(_identity);
                Tracker.connect();
            })
            .catch(function () {
                _identity = null;
                _authenticated = false;
                deferred.resolve(_identity);
            });
        return deferred.promise; 

, reset {} , .

0

Source: https://habr.com/ru/post/1607618/


All Articles