skip to Main Content

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",
          },
        ],
      };
     }

2

Answers


  1. Chosen as BEST ANSWER

    I just forgot the curly brackets when passing res and req throw the context


  2. you need to pass context something like this and you are good to go

     context: ({ req, res }): Context => ({
        req,
        res,
        session: req.session,
      }),
    

    also, it’s better to do the configuration in the GraphQL config file to include the credentials.

    graphql.config.ts

    import { ApolloDriverConfig, ApolloDriver } from '@nestjs/apollo';
    import { join } from 'path';
    
    export const GraphQLConfig: ApolloDriverConfig = {
      driver: ApolloDriver,
      debug: true,
      autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
      playground: {
        settings: {
          'editor.theme': 'light', // use value dark if you want a dark theme in the playground
          'request.credentials': 'include',
        },
      },
    };
    

    and assign the config file to the module directory

    user.module.ts

    import { Module } from '@nestjs/common';
    import { GraphQLModule } from '@nestjs/graphql';
    import { TypeOrmModule } from '@nestjs/typeorm';
    import { GraphQLConfig } from 'src/config/graphql.config';
    import { UserEntity } from 'src/entity/user.entity';
    import { UserResolver } from './user.resolver';
    import { UserService } from './user.service';
    
    @Module({
      imports: [
        TypeOrmModule.forFeature([UserEntity]),
        GraphQLModule.forRoot(GraphQLConfig),
      ],
      providers: [UserService, UserResolver],
    })
    export class UserModule {}
    

    and it will automatically enable credentials value to be "include" from "omit"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search