I have a problem. While setting up the session in Express and GraphQL.
Config:
app.use(
session({
name: "user",
secret:
"test",
resave: false,
saveUninitialized: false,
cookie: {
secure: false,
expires: 60 * 60 * 10000,
},
})
)
app.use(
cors({
origin: ["http://localhost:3000"],
methods: ["GET", "POST"],
credentials: true,
})
)
app.use(express.json())
app.use(cookieParser())
app.use(bodyParser.urlencoded({ extended: true }))
GraphQL route(How I get req as a parameter):
graphqlHTTP({
schema,
graphiql: true,
context: (req: any) => ({
...req,
}),
})
How I define a session:
const getId = await Users.findOne({ user_email: email })
const ID = getId?.user_id
req.session.user = ID
console.log("ee " + req.session.user)
Check if session is there:
try {
if (req.session.user) {
return {
success: true,
user_id: req.session.user,
}
} else {
return { success: false }
}
} catch (error) {
console.log(error)
}
And it returns Cannot set properties of undefined (setting 'user')
What is the problem?
Is it that, how I get req as a parameter, or anything else?