CloudFlare set-cookie 未传递到浏览器

CloudFlare set-cookie not passing through to browser

我有一个支持 CloudFlare 的快速应用程序。在开发中,Set-Cookie header 在登录时通过,但在 CloudFlare 之后,Set-Cookie header 未通过。有什么建议么?我的配置是:

``` 从 'passport' 导入护照; 从 'cookie-parser' 导入 cookieParser; 从 'express-session' 导入 session; 从 'passport-local' 导入 { Strategy as LocalStrategy}; 从 '../../db/sequelize/passport';

导入 localPassport
const secret = 'foo';

const authenticationMiddleware = (req, res, next) => {
  console.log('authenticated', req.isAuthenticated());

  if (req.isAuthenticated()) {
    next();
  } else if (req.url.includes('/rest/')) {
    res.status(401).send('Unauthorized');
  } else {
    res.status(302).redirect('/admin/login');
  }
};

const sessionSecurity = (app) => {
  app.set('trust proxy', 1);
  app.use(cookieParser(secret));
  app.use(
    session({
      secret,
      proxy: true, 
      saveUninitialized: false,
      resave: false,
      maxAge: null,
      cookie: {
        path: '/admin',
        secure: process.env.NODE_ENV === 'production',
      },
    }),
  ); // session secret
  app.use(passport.initialize());
  app.use(passport.session());

  passport.use(new LocalStrategy(localPassport.local));
  passport.serializeUser((user, done) => {
    done(null, user.id);
  });
  passport.deserializeUser(localPassport.deserializeUser);

  app.post('/admin/login', (req, res, next) => {
    passport.authenticate('local', (authErr, user) => {
      if (authErr) return next(new Error(authErr));
      if (!user) {
        return res.sendStatus(401);
      }
      return req.logIn(user, (loginErr) => {
        if (loginErr) return res.sendStatus(401);
        return res.sendStatus(200);
      });
    })(req, res, next);
  });

  app.post('/admin/logout', (req, res) => {
    req.logOut();

    req.session.destroy(() => {
      res
        .clearCookie('connect.sid', { path: '/admin' })
        .sendStatus(200);
    });
  });

  app.get('/admin/rest/*', authenticationMiddleware);

  app.get('/admin/rest/status', (req, res) => {
    res.sendStatus(200);
  });

};

```

这是因为服务器设置设置为 cookie 安全,而 CloudFlare 和我的服务器之间的连接不是。

在 cloudflare(加密选项卡)的设置中,我打开了 "Authenticated Origin Pulls" 并在 "app.js" 中(从我的应用程序)添加了这个 app.set('trust proxy', 1);app.use(session({...}) 之前我的问题就解决了。