Sails.js + Passport.js获取当前登录的用户 [英] Sails.js + Passport.js getting current logged in user
问题描述
我有我的工作一帆应用程序,我使用的用户身份验证passport.js。我已经登录的工作,以保护我的网页政策以来,所有的系统去。在我的网页在用户输入信息之一,所以我需要登录的用户的id的电流。我挣扎与此,但最终想出了用路线
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
在控制器
$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);
});
});
});
和路线回调
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)这是这样做的正确方法?经由路径馈送这个信息到控制器?
a) is this the correct way to do this? Feeding this information to the controller via a route?
b)是嵌套套接字调用这样好的做法呢?
b) is nesting socket calls like this good practice?
我是新来这个堆栈和仍然感到通过它我的方式,所以任何反馈是大大AP preciated。
I'm new to this stack and still feeling my way through it, so any feedback is greatly appreciated.
推荐答案
尝试:
get_user: function(req, res) {
if ( !req.isAuthenticated() ) return res.forbidden();
return res.json({user: req.user});
}
这篇关于Sails.js + Passport.js获取当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!