add password.interceptor

This commit is contained in:
秦秋旭 2023-02-17 16:13:04 +08:00
parent b9f18ccc5d
commit a56e4be200
3 changed files with 25 additions and 2 deletions

View File

@ -0,0 +1,22 @@
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common'
import { map, Observable } from 'rxjs'
import { UserEntity } from 'src/users/entities/user.entity'
@Injectable()
export class PasswordInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
map((user: UserEntity) => {
if ('password' in user) {
user.password = ''
}
return user
}),
)
}
}

View File

@ -1,14 +1,16 @@
import { Controller, Get } from '@nestjs/common' import { Controller, Get, UseInterceptors } from '@nestjs/common'
import { UsersService } from './users.service' import { UsersService } from './users.service'
import { ApiTags } from '@nestjs/swagger' import { ApiTags } from '@nestjs/swagger'
import { User } from 'src/common/decorators/user.decorator' import { User } from 'src/common/decorators/user.decorator'
import { NeedAuth } from 'src/common/decorators/need-auth.decorator' import { NeedAuth } from 'src/common/decorators/need-auth.decorator'
import { PasswordInterceptor } from 'src/common/interceptors/password.interceptor'
@ApiTags('User') @ApiTags('User')
@Controller('api/user') @Controller('api/user')
export class UsersController { export class UsersController {
constructor(private readonly userService: UsersService) {} constructor(private readonly userService: UsersService) {}
@UseInterceptors(PasswordInterceptor)
@NeedAuth() @NeedAuth()
@Get('profile') @Get('profile')
async getUserInfo(@User('userId') userId: string) { async getUserInfo(@User('userId') userId: string) {

View File

@ -32,7 +32,6 @@ export class UsersService {
password: hashedPassword, password: hashedPassword,
}, },
}) })
delete user.password
return user return user
} }
} }