简体   繁体   English

如何在 NestJS GraphQL 应用程序中设置 cookies 以响应 object?

[英]How can I set cookies in response object in a NestJS GraphQL application?

How can I set cookies in response object in a NestJS GraphQL application?如何在 NestJS GraphQL 应用程序中设置 cookies 以响应 object? Code:代码:

app.module.ts: app.module.ts:

GraphQLModule.forRoot<ApolloDriverConfig>({
      autoSchemaFile: true,
      driver: ApolloDriver,
      cors: {
        origin: process.env.ADDRESS,
        credentials: true,
      },
      context: ({ req, res }) => ({ req, res }),
    }),

resolver:解析器:

 @Mutation(() => token)
  async login(
    @Args("input") input: LoginI,
    @Context() context: GraphQLExecutionContext
  ) {
    const result = await this.authnService.login(input);
    context.res.cookie("authorization", "Bearer " + result.userToken);
    return result;
  }

But it doesn't seem to work correctly.但它似乎无法正常工作。

console.log(context.res.cookies)

returns undefined.返回未定义。 Also there are no cookies in browser devtools for my client.我的客户端的浏览器开发工具中也没有 cookies。

You cannot set Cookies into response .您不能将 Cookie 设置为response You must set Cookies in the request .您必须在请求中设置 Cookie。

The @Res() decorator is imported from the @nestjs/common, while Response from the express package.

@Post('some-route')
     someHandler(@Res({ passthrough: true }) response: Response) {
     # get create refreshToken
     response.cookie('refreshToken', refreshToken);
   }
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM