How to get user's information in Nest.js?

前端 未结 3 2145
小鲜肉
小鲜肉 2021-01-20 22:56

I am using Angular+Nest to develop a website. I have created a service(Angular) so that client can get user\'s information from server when project start up(the same as fresh).

3条回答
  •  既然无缘
    2021-01-20 23:33

    If you really insist on this way (see comments), you can use Interceptors:

    
    @Injectable()
    export class GetUserInterceptor implements NestInterceptor {
      constructor(private readonly authService: AuthService) {
      }
    
      async intercept(context: ExecutionContext, next: CallHandler) {
        const request = context.switchToHttp().getRequest()
        const item = await this.authService.getByToken(/* extract me from headers*/)
        request.user = item
        return next.handle()
      }
    }
    
    

    so AuthGuard is not needed.

提交回复
热议问题