7
votes

I have a sails app that I'm working with and I'm using passport.js for user auth. I have login working, along with policies that protect my pages, all systems go. On one of my pages the user is inputting information, so I need to get the current logged in user's id. I struggled with this but eventually came up with using a route

In the controller

 $scope.$on('sailsSocket:connect', function(ev, data) {
    sailsSocket.get(
    '/get_user', {},
    function(response) {
        $scope.team_id = response.user;
        sailsSocket.get(
        '/status_update?sort=createdAt%20ASC&team_id='+$scope.team_id, {},
        function(response) {
            $scope.updates = response;
            $log.debug('sailsSocket::/status_update', response);
        });
    });
});

and the route callback

get_user: function(req, res) {
    res.json({user:req.session.passport.user});
},

This seems to be working correctly, but I do have a few questions.

a) is this the correct way to do this? Feeding this information to the controller via a route?

b) is nesting socket calls like this good practice?

I'm new to this stack and still feeling my way through it, so any feedback is greatly appreciated.

1
Your question is very confusing.. But you can simply access the logged in user through req.user. - Adil Malik

1 Answers

6
votes

Try:

get_user: function(req, res) {
    if ( !req.isAuthenticated() ) return res.forbidden();

    return res.json({user: req.user});
}