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

寵の児 提交于 2019-12-07 00:06:12

问题


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:


Try:

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

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


来源:https://stackoverflow.com/questions/22107715/sails-js-passport-js-getting-current-logged-in-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!