dmh*_*126 10 javascript authentication passport.js angular
从后端到前端发送护照会话信息的最佳方法是什么?
我的应用程序适用于端口3000.前两个获取是用于Facebook登录和重定向.下一步是从数据库中获取用户数据(用户ID应存储在其中req.user
)
routes.js:
app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : 'http://localhost:8000/',
failureRedirect : '/fail'
})
);
app.get('/auth/userdata', isLoggedIn, function(req, res) {
Donator.findById(req.user, function(err, fulluser) {
if (err) throw err;
res.json(fulluser);
})
});
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
next();
} else {
res.json(false);
}
};
Run Code Online (Sandbox Code Playgroud)
护照config.js
'facebookAuth' : {
'clientID' : 'secret',
'clientSecret' : 'secret',
'callbackURL' : 'http://localhost:3000/auth/facebook/callback'
},
Run Code Online (Sandbox Code Playgroud)
所以在我的Angular2应用程序中,我可以转到http://localhost:3000/auth/facebook
,重定向到FB登录页面,如果成功重定向到http://localhost:3000/auth/login/callback
哪个带我到http://localhost:8000/
.
在我的Angular2应用程序中,适用于端口8000
getUser(){
this.http.get('http://localhost:3000/auth/userdata')
.map(res => return res.json())
}
Run Code Online (Sandbox Code Playgroud)
每次getUser()
调用它都会返回'false'.是否有一种简单而安全的方法将此会话数据"注入"到我的前端不同的端口?此外,当我进入http://localhost:3000/auth/userdata
浏览器时,我可以看到此配置文件呈现为JSON.
当我设置后端和前端在同一个端口它工作,Facebook,Twitter,谷歌,本地,一切都很好,并getUser
返回完整的用户配置文件.
我希望它很清楚.
Angular2中的请求存在问题.我已为每个请求添加了凭据:
getUser(){
this.http.get('http://localhost:3000/auth/userdata', {withCredentials: true})
.map(res => return res.json())
}
Run Code Online (Sandbox Code Playgroud)
现在好了.
归档时间: |
|
查看次数: |
3686 次 |
最近记录: |