I'm trying to create a login system with GraphQL and Express, but the sessions aren't being saved. Whenever I log in, req.session.userId
stays undefined.
My code:
(async () => {
await connect(process.env.MONGO_URI!, { dbName: "example" });
const app = express();
app.use(
cors({
origin: [__clientUrl__, "https://studio.apollographql.com"],
credentials: true
})
);
app.use(
session({
name: "qid",
secret: process.env.SESSION_SECRET!,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI,
dbName: "example"
}),
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 6.048e8,
httpOnly: __prod__,
sameSite: "lax",
secure: __prod__
}
})
);
const server = new ApolloServer({
schema: await buildSchema({
resolvers: [HelloResolver, UserResolver],
validate: false
}),
context: ({ req, res }) => ({ req, res })
});
await server.start();
server.applyMiddleware({
app,
cors: {
origin: [__clientUrl__, "https://studio.apollographql.com"],
credentials: true
}
});
app.listen(__port__, () =>
console.log(
`