无法访问中间件中的固定cookie

时间:2019-05-06 02:07:54

标签: nestjs

我尝试设置req用户:

use(req: any, res: Response, next: () => {}) {
    req.user = this.userService.getUserAuthenticated(req.cookies.t);
    next();
}

但是req.cookies没有定义。

使用以下方式启用Cookie:

app.register(require('fastify-cookie'));

ExecutionContext中使用Interceptor可以很好地工作,但是我需要葫芦色,并且需要canActivate在Interceptor之前调用。

1 个答案:

答案 0 :(得分:0)

在应用程序lifecycle of fastify中的onRequest挂钩中对cookie进行了解析,因此如果未定义,则您的使用之前发生,或者您没有cookie。

你能做什么?

1)从headers.raw手动将cookie切成小块。

2)用作cookie的onRequest句柄之后的钩子。