Express会话无法通过Docker在Graphql中持久化会话

时间:2018-12-17 16:02:08

标签: docker express graphql express-session typeorm

我使用graphql-yoga创建我的graphql api,express-session配置是这样的:

<div [popover]="popTemplate" #pop="bs-popover" style="background-color:black" triggers="" (mouseenter)="pop.show()">This is a test</div>

<ng-template #popTemplate>
  <div (mouseleave)="pop.hide()">
    <p>Popover content.</p>
    <button type="submit">Click me</button>
  </div>     
</ng-template>

完整的代码here

然后,如果有任何用户想要登录,则按如下方式保存会话:

session({
  store: new RedisStore({
    client: redis as any,
    prefix: redisSessionPrefix
  }),
  name: "qid",
  secret: process.env.SESSION_SECRET as string,
  resave: false,
  saveUninitialized: false,
  cookie: {
    httpOnly: true,
    secure: process.env.NODE_ENV === "production", // Only works with https
    maxAge: 1000 * 60 * 60 * 24 * 7 // 7 days
  }
})

session.userId = user.id; 来自一个称为TypeORM的ORM,仅通过电子邮件查找用户,我将该用户的ID放在user.id中。完整的代码here

然后,如果要检查用户是否已登录,请使用查询:

session.userId

之前的代码仅从TypeORM执行名为User的实体,该实体仅查找具有User.findOne({ where: { id: session.userId } }) 的用户。但是session.userId是不确定的...完整代码here

有人知道为什么吗?

这些是我用于开发的docker配置。

Dockerfile.dev Docker-compose.dev.yml

如果有人需要100%完整的代码,则here可以找到它,然后可以使用session.userId在服务器文件夹中运行它。

1 个答案:

答案 0 :(得分:1)

如果有人想知道,Twitter的Ben Awad帮助了我,解决方案是在graphql-playground request.credentials: include中添加,就是这样:)

enter image description here

相关问题