I am having trouble with sessions. For some reason, the req.session is undefined even though I'm using the session middleware. I was trying to use redis but I couldn't make the connection work. The strange part is that for some reason the cookie is registered in the graphql playground. So the reason must be in the way I pass the request, probably.
All the types are correct ( typescript isn't angry with anything ).
Here's the code from the server.ts
import express, { Request, Response } from "express";
import routes from "./routes";
import cors from "cors";
import "reflect-metadata";
import { createConnection } from "typeorm";
import { ApolloServer } from "apollo-server-express";
import { buildSchema } from "type-graphql";
import session from "express-session";
createConnection()
.then(async (connection) => {
console.log("Conexão feita com sucesso");
const app = express();
app.set("trust proxy", 1);
app.use(cors());
app.use(
session({
name: "qid",
secret: "keyboard cat",
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: 1000 * 60 * 60 * 24 * 365 * 10,
httpOnly: true,
},
})
);
const apolloServer = new ApolloServer({
schema: await buildSchema({
resolvers: [
],
validate: false, // Activate the validation with class-validation module.
}),
context: (req: Request, res: Response): Context => ({ req, res, session: req.session }),
playground: {
settings: {
'request.credentials': 'include',
},
},
});
apolloServer.applyMiddleware({ app });
app.use(express.json());
app.use(routes);
app.listen(3333);
})
.catch((error) => console.error(error));
And where I use the session.
@Mutation(() => UserResponse)
async login(
@Arg("info", () => UserLoginInputType) info: UserLoginInputType,
@Ctx(){req, res, session}: Context
): Promise<UserResponse> {
const user = await User.findOneOrFail({ where: { email: info.email } });
const valid = await argon2.verify(user.password, info.password);
if (valid) {
req.session.userId = user.id;
return {
user,
};
}
return {
errors: [
{
field: "password",
message: "Incorrect password",
},
],
};
}