English 中文(简体)
Can t set cookie in response NestJs
原标题:

I Have a nestjs graphQL auth microservice and trying to set a http only cookie containing the token, and for some reason setting the cookie doesn t work, i get this error:

{
  "errors": [
    {
      "message": "response.cookie is not a function",
      "path": [
        "login"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "GraphQLError: response.cookie is not a function",
          "    at downstreamServiceError (/home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:872:12)",
          "    at /home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:577:9",
          "    at Array.map (<anonymous>)",
          "    at sendOperation (/home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:576:38)",
          "    at processTicksAndRejections (node:internal/process/task_queues:95:5)",
          "    at /home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:450:41",
          "    at executeNode (/home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:372:9)",
          "    at /home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:195:27",
          "    at /home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/index.ts:826:28",
          "    at execute (/home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/server/src/requestPipeline.ts:536:22)"
        ],
        "serviceName": "users"
      }
    }
  ],
  "data": null
}

Here is main.ts

async function bootstrap() {
  const app: NestExpressApplication =
    await NestFactory.create<NestExpressApplication>(
      UsersModule,
      new ExpressAdapter(),
    );

  app.use(cookieParser());

  return app.listen(9009);
}

bootstrap();

and this is my auth resolver

@Resolver( Auth )
export class AuthResolver {
  constructor(private authService: AuthService) {}

  @Query( login )
  async login(
    @Res() response: Response,
    @Args( user ) user: LoginUserInput,
  ): Promise<LoginResult> {
    // log(res.cookie( test , 33));

    try {
      const result = await this.authService.validateUserByPassword(user);

      if (result) {
        response.cookie( access_token , result.token);
        return result;
      }
      throw new AuthenticationError(
         Could not log-in with the provided credentials ,
      );
    } catch (err) {
      throw err;
    }
  }

}

i can t seem to figure out why as for my point of view everything should be working.

I tried fastify platform, with fastify cookie and I am getting a similar error. I am expecting to set the cookie

问题回答

@Res() is not a valid decorator for GraphQL resolvers. You should instead use @Context() to get the context object and pull the res object from there.

@Context() { res } or @Context() ctx => ctx.res should do it. You may need to modify your GraphQlModule.forRoot() to include context: ({ req, res }) => ({ req, res })





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签