我有一个由JWT保护的NestJS后端。我想知道存储实际用户ID的最佳方法是什么?我有一个JwtAuthGuard
@Injectable()
export class JwtAuthGuard extends AuthGuard( 'jwt' ) {
canActivate(context: ExecutionContext) {
return super.canActivate( context );
}
handleRequest(err, user, info) {
if ( err || !user ) {
throw err || new UnauthorizedException();
}
return user;
}
}
我的实际用户ID在handleRequest中的var用户中,但是我不知道在哪里可以“存储”它,以便在某些模块中可以访问它。也许有主题服务?我不知道。有人可以帮助我吗?
谢谢
您可以使用中间件Middleware是在路由处理程序之前调用的函数。check the documentation