Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular2中的Passport JS会话数据_Javascript_Angular_Authentication_Passport.js - Fatal编程技术网

Javascript Angular2中的Passport JS会话数据

Javascript Angular2中的Passport JS会话数据,javascript,angular,authentication,passport.js,Javascript,Angular,Authentication,Passport.js,将passport会话信息从后端发送到前端的最佳方式是什么 我的应用程序在端口3000上工作。前两个GET用于facebook登录和重定向。下一步是从数据库获取用户数据(用户id应存储在req.user) routes.js: app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); app.get('/auth/facebook/callback', passport.

将passport会话信息从后端发送到前端的最佳方式是什么

我的应用程序在端口3000上工作。前两个GET用于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);
    }
};
passport config.js

'facebookAuth' : {
        'clientID'      : 'secret',
        'clientSecret'  : 'secret',
        'callbackURL'   : 'http://localhost:3000/auth/facebook/callback'
    },
因此,在我的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())
}
每次调用
getUser()
,它都返回“false”。是否有一种简单而安全的方法将此会话数据“注入”到不同端口的前端?当我去
http://localhost:3000/auth/userdata
在浏览器中,我可以看到此配置文件呈现为JSON

当我将后端和前端设置在它工作的同一个端口上时,facebook、twitter、google、local,一切正常,
getUser
返回完整的用户配置文件


我希望这是清楚的。

这是Angular2中的请求问题。我已将凭据添加到每个请求:

getUser(){
    this.http.get('http://localhost:3000/auth/userdata', {withCredentials: true})
    .map(res => return res.json())
}

现在一切都好了。

为什么不在端口
3000
上提供angular2应用程序?我现在希望前端和后端在不同的主机上运行。我建议在同一端口上运行所有程序。您可以在不同的端口上有两台服务器,但可以使用代理将其中一台服务器代理到另一台。这可能是一个跨源资源共享(CORS)问题。您可以在web浏览器中检查开发人员控制台的“网络”选项卡吗?看看“getUser()”对请求的响应,是否有错误?是否没有错误。它的行为就像来自完全不同的地方的请求;在服务器中,我得到req.isAuthenticated()false。如何解决这个问题?我还有
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE,OPTIONS')在我的cors选项中。它正在工作,同样对于POST请求,您应该包括如下选项:
options=newrequestoptions({headers:headers,withCredentials:true})