如何在嵌套中间件中设置cookie和使用上下文

时间:2019-07-20 12:46:37

标签: node.js express nestjs

我最近使用nestjs。并且我编写了一个auth用户中间件,我想设置cookie并将用户信息设置为上下文。但是req.cookie()不存在, req.context类型错误

节点8.9 嵌套最新

@Injectable()
export class AuthUserMiddleware implements NestMiddleware {
  constructor(private readonly userService: UserService) {

  }
  async use(req: Request, res: Response, next: Function) {
    if (!req.context) {
      req.context = {};
    }
    const uid = req.cookies.uid;
    const user = await this.userService.refreshUser(uid);
    req.cookies("uid", user.uid, {
      httpOnly: false,
      domain: config.domain,
      path: "/"
    });
    req.context.user = user;
    next();
  }
}

enter image description here

0 个答案:

没有答案