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 { ApiTags } from '@nestjs/swagger' | ||||
| import { User } from 'src/common/decorators/user.decorator' | ||||
| import { NeedAuth } from 'src/common/decorators/need-auth.decorator' | ||||
| import { PasswordInterceptor } from 'src/common/interceptors/password.interceptor' | ||||
| 
 | ||||
| @ApiTags('User') | ||||
| @Controller('api/user') | ||||
| export class UsersController { | ||||
|   constructor(private readonly userService: UsersService) {} | ||||
| 
 | ||||
|   @UseInterceptors(PasswordInterceptor) | ||||
|   @NeedAuth() | ||||
|   @Get('profile') | ||||
|   async getUserInfo(@User('userId') userId: string) { | ||||
|  | ||||
| @ -32,7 +32,6 @@ export class UsersService { | ||||
|         password: hashedPassword, | ||||
|       }, | ||||
|     }) | ||||
|     delete user.password | ||||
|     return user | ||||
|   } | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user