delete user
This commit is contained in:
		
							parent
							
								
									f1457864df
								
							
						
					
					
						commit
						5bc8a7e072
					
				| @ -5,6 +5,7 @@ export enum EmailScene { | |||||||
|   register = 'register', |   register = 'register', | ||||||
|   updatePassword = 'updatePassword', |   updatePassword = 'updatePassword', | ||||||
|   updateEmail = 'updateEmail', |   updateEmail = 'updateEmail', | ||||||
|  |   deleteUser = 'deleteUser', | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export class EmailSendDto { | export class EmailSendDto { | ||||||
|  | |||||||
| @ -11,6 +11,7 @@ export class EmailService { | |||||||
|     [EmailScene.register]: '注册账号', |     [EmailScene.register]: '注册账号', | ||||||
|     [EmailScene.updatePassword]: '修改密码', |     [EmailScene.updatePassword]: '修改密码', | ||||||
|     [EmailScene.updateEmail]: '修改邮箱', |     [EmailScene.updateEmail]: '修改邮箱', | ||||||
|  |     [EmailScene.deleteUser]: '删除用户', | ||||||
|   } |   } | ||||||
|   constructor( |   constructor( | ||||||
|     private prismaService: PrismaService, |     private prismaService: PrismaService, | ||||||
| @ -31,11 +32,8 @@ export class EmailService { | |||||||
|         } |         } | ||||||
|         break |         break | ||||||
|       case EmailScene.updatePassword: |       case EmailScene.updatePassword: | ||||||
|         await this.prismaService.user.findUniqueOrThrow({ |  | ||||||
|           where: { email }, |  | ||||||
|         }) |  | ||||||
|         break |  | ||||||
|       case EmailScene.updateEmail: |       case EmailScene.updateEmail: | ||||||
|  |       case EmailScene.deleteUser: | ||||||
|         await this.prismaService.user.findUniqueOrThrow({ |         await this.prismaService.user.findUniqueOrThrow({ | ||||||
|           where: { email }, |           where: { email }, | ||||||
|         }) |         }) | ||||||
|  | |||||||
							
								
								
									
										17
									
								
								src/users/dto/delete-user.dto.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/users/dto/delete-user.dto.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,17 @@ | |||||||
|  | import { IsEmail, IsNotEmpty, IsStrongPassword } from 'class-validator' | ||||||
|  | 
 | ||||||
|  | export class DeleteUserDto { | ||||||
|  |   @IsNotEmpty() | ||||||
|  |   @IsEmail() | ||||||
|  |   email: string | ||||||
|  | 
 | ||||||
|  |   @IsNotEmpty() | ||||||
|  |   verifyCode: string | ||||||
|  | 
 | ||||||
|  |   @IsNotEmpty() | ||||||
|  |   token: string | ||||||
|  | 
 | ||||||
|  |   @IsNotEmpty() | ||||||
|  |   @IsStrongPassword() | ||||||
|  |   password: string | ||||||
|  | } | ||||||
| @ -2,6 +2,7 @@ import { | |||||||
|   Controller, |   Controller, | ||||||
|   Get, |   Get, | ||||||
|   Post, |   Post, | ||||||
|  |   Delete, | ||||||
|   Patch, |   Patch, | ||||||
|   Body, |   Body, | ||||||
|   UseInterceptors, |   UseInterceptors, | ||||||
| @ -15,6 +16,7 @@ import { PrismaService } from 'nestjs-prisma' | |||||||
| import { UserEntity } from './entities/user.entity' | import { UserEntity } from './entities/user.entity' | ||||||
| import { CreateUserDto } from './dto/create-user.dto' | import { CreateUserDto } from './dto/create-user.dto' | ||||||
| import { UpdatePassword } from './dto/update-password.dto' | import { UpdatePassword } from './dto/update-password.dto' | ||||||
|  | import { DeleteUserDto } from './dto/delete-user.dto' | ||||||
| 
 | 
 | ||||||
| @ApiTags('User') | @ApiTags('User') | ||||||
| @Controller('api/users') | @Controller('api/users') | ||||||
| @ -38,6 +40,13 @@ export class UsersController { | |||||||
|     return this.userService.register(userData) |     return this.userService.register(userData) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   @NeedAuth() | ||||||
|  |   @ApiOperation({ summary: '删除用户' }) | ||||||
|  |   @Delete('me') | ||||||
|  |   async deleteUser(@Body() userData: DeleteUserDto) { | ||||||
|  |     return this.userService.deleteUser(userData) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   @ApiOperation({ summary: '修改密码' }) |   @ApiOperation({ summary: '修改密码' }) | ||||||
|   @UseInterceptors(PasswordInterceptor) |   @UseInterceptors(PasswordInterceptor) | ||||||
|   @Patch('me/password') |   @Patch('me/password') | ||||||
|  | |||||||
| @ -8,6 +8,8 @@ import { EmailSendDto, EmailScene } from 'src/email/dto/email.dto' | |||||||
| import { EmailService } from 'src/email/email.service' | import { EmailService } from 'src/email/email.service' | ||||||
| import { TokenService } from './token.service' | import { TokenService } from './token.service' | ||||||
| import { UpdatePassword } from './dto/update-password.dto' | import { UpdatePassword } from './dto/update-password.dto' | ||||||
|  | import { DeleteUserDto } from './dto/delete-user.dto' | ||||||
|  | 
 | ||||||
| @Injectable() | @Injectable() | ||||||
| export class UsersService { | export class UsersService { | ||||||
|   constructor( |   constructor( | ||||||
| @ -40,6 +42,24 @@ export class UsersService { | |||||||
|     return this.tokenService.generateTokens({ userId: user.id }) |     return this.tokenService.generateTokens({ userId: user.id }) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   async deleteUser(userData: DeleteUserDto) { | ||||||
|  |     await this.verifyEmail( | ||||||
|  |       userData.email, | ||||||
|  |       userData.token, | ||||||
|  |       userData.verifyCode, | ||||||
|  |       EmailScene.deleteUser, | ||||||
|  |     ) | ||||||
|  |     const user = await this.prismaService.user.findUnique({ | ||||||
|  |       where: { email: userData.email }, | ||||||
|  |     }) | ||||||
|  |     const passwordValid = await bcrypt.compare(user.password, userData.password) | ||||||
|  |     if (!passwordValid) { | ||||||
|  |       throw new ForbiddenException('Invalid password') | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return this.prismaService.user.delete({ where: { email: userData.email } }) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   async updatePassword(payload: UpdatePassword) { |   async updatePassword(payload: UpdatePassword) { | ||||||
|     await this.verifyEmail( |     await this.verifyEmail( | ||||||
|       payload.email, |       payload.email, | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user