Search code examples
dockerexpressgraphqlexpress-sessiontypeorm

Express session does not persist session in Graphql over Docker


I use graphql-yoga to create my graphql api, the express-session config is this:

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
  }
})

Complete code here.

Then, if any user wants to log in, the session is saved like this:

session.userId = user.id;

The user.id is from an ORM called TypeORM, only find a user by the email, and I put the id of that user in the session.userId. Complete code here.

And then, if I want to check if a user is logged in or not, I use the Query:

User.findOne({ where: { id: session.userId } })

That previous code only execute the Entity from TypeORM called User, which only find what user has the session.userId. But that session.userId is undefined... Complete code here

Someone know why?

These are the docker configs that I use for development.

Dockerfile.dev Docker-compose.dev.yml

If someone need the 100% complete code, here you can find it and you can run it with docker-compose -f docker-compose.dev.yml up --build within the server folder.


Solution

  • If someone want to know, Ben Awad from Twitter helped me, the solution was to add in the graphql-playground request.credentials: include, and that's it :)

    enter image description here