【问题标题】:Passport.js Session is lost when CORS call in heroku but don't in local当 CORS 在 heroku 中调用但不在本地时,Passport.js 会话丢失
【发布时间】:2017-10-26 03:30:36
【问题描述】:

大家好,我有一个 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 应用程序。并且在第一次通话中设置了会话,但在其他通话中不再存在。我错过了什么吗?

【问题讨论】:

    标签: angular express heroku


    【解决方案1】:
    猜你喜欢
    • 2013-10-21
    • 2016-05-13
    • 1970-01-01
    • 2014-03-21
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2014-03-21
    相关资源
    最近更新 更多