add password.interceptor
This commit is contained in:
parent
b9f18ccc5d
commit
a56e4be200
22
src/common/interceptors/password.interceptor.ts
Normal file
22
src/common/interceptors/password.interceptor.ts
Normal 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
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -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) {
|
||||||
|
@ -32,7 +32,6 @@ export class UsersService {
|
|||||||
password: hashedPassword,
|
password: hashedPassword,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
delete user.password
|
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user