在heroku中调用CORS时,Passport.js会话丢失但不在本地

时间:2017-10-23 19:44:13

标签: angular express heroku

大家好,我有一个Angular 4应用程序在本地计算机上调用Express API,这些应用程序在不同的端口(4200和3000)运行。在我配置的Express应用程序中:

app.use((req, res, next) => {
    console.log(req.headers.origin);
    res.header('Access-Control-Allow-Origin', req.headers.origin);
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
    if ('OPTIONS' == req.method) {
        res.sendStatus(200);
    } else {
        next();
    }
});

和我的cookie配置:

app.use(session({
    resave: true,
    saveUninitialized: true,
    secret: process.env.SESSION_SECRET,
    cookie: {
        httpOnly: true,
        expires: cookieExpirationDate // use expires instead of maxAge
    }
}));

在Angular 4中,使用

调用请求
this.http.post(environment.services + '/login', user, {withCredentials: true});

一切都在localhost中运行。但随后这些应用程序被部署到不同的heroku应用程序。会话在第一个呼叫中设置,但在其他呼叫中不再存在。我想念一下吗?

1 个答案:

答案 0 :(得分:0)