简体   繁体   English

如何使用cors起源和角度的会话?

[英]How to use session with cors origin and angular?

My session work fine until i tried to allow cors. 我的会议工作正常,直到我试图允许角色。

Server: 服务器:

  app.use(require('express-session')({
    secret: 'some key',
    resave: true,
    saveUninitialized: true,
    proxy: true,
    cookie: {
      secure: false,
      httpOnly: false,
    },
  }));

 app.use(require('cors')());
 @Get('getUserAuthenticated')
 async getUserAuthenticated(@Req() req): Promise<{ user: User }> {
    return { user: req.user };
 }

And i use proxy: 我使用代理:

{
    "/rest": {
      "target": "http://localhost:3000",
      "secure": true,
      "logLevel": "debug",
      "changeOrigin": true,
    }
}

getUserAuthenticated is empty when i using cors. 当我使用cors时, getUserAuthenticated为空。 i need it to develop purpose, so what i miss else? 我需要它来发展目的,所以我想念别的吗?

you should use 你应该使用

{ origin:true, withCredentials:true }

this allows to pass cookies arround 这允许传递cookies arround

For someone that trying to do the same: 对于试图做同样事情的人:

server: 服务器:

  const corsOptions = {
    origin: 'http://localhost:4200',
    optionsSuccessStatus: 200,
    credentials: true,
  };
  app.use(require('cors')(corsOptions));

client: 客户:

get(url) { return this.httpClient.get(url, { withCredentials: true })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM