Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js + Passport.js getting current logged in user

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.

like image 211
user2812463 Avatar asked Feb 28 '14 23:02

user2812463


1 Answers

Try:

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

    return res.json({user: req.user});
}
like image 195
Alberto Souza Avatar answered Oct 21 '22 07:10

Alberto Souza