From a00990b6d692b74dca831e86f84dd0f6cb6cf653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E7=A7=8B=E6=97=AD?= Date: Thu, 23 Feb 2023 23:01:51 +0800 Subject: [PATCH] changeEmail --- src/email/email.service.ts | 7 ++++--- src/users/dto/change-email.dto.ts | 13 +++++++++++++ src/users/me.controller.ts | 17 ++++++++++++----- src/users/me.service.ts | 15 +++++++++++++++ src/users/users.controller.ts | 2 +- 5 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 src/users/dto/change-email.dto.ts diff --git a/src/email/email.service.ts b/src/email/email.service.ts index 2265611..e763495 100644 --- a/src/email/email.service.ts +++ b/src/email/email.service.ts @@ -10,6 +10,7 @@ import { MailerService } from '@nestjs-modules/mailer' import { JwtService } from '@nestjs/jwt' import { PrismaService } from 'nestjs-prisma' import { EmailSendDto, EmailScene } from 'src/email/dto/email.dto' +import { UserEntity } from 'src/users/entities/user.entity' @Injectable() export class EmailService { @@ -28,7 +29,7 @@ export class EmailService { ) {} async sendEmailToken(email: string, scene: EmailScene) { - const user = await this.prismaService.user.findUnique({ + const user: UserEntity | null = await this.prismaService.user.findUnique({ where: { email }, }) switch (scene) { @@ -47,7 +48,7 @@ export class EmailService { } const verifyCode = this.generateVerifyCode() - const registerToken = this.jwtService.sign( + const token = this.jwtService.sign( { email, scene }, { secret: this.getEmailJwtSecret(verifyCode, scene) }, ) @@ -56,7 +57,7 @@ export class EmailService { subject: `【qiuxu.site】${this.subjectMap[scene]}`, html: `您正在qiuxu.site${this.subjectMap[scene]},验证码为 ${verifyCode},30分钟内有效`, }) - return { registerToken, userId: user.id } + return { token, userId: user?.id } } // TODO: 做成守卫? diff --git a/src/users/dto/change-email.dto.ts b/src/users/dto/change-email.dto.ts new file mode 100644 index 0000000..8f6dd9a --- /dev/null +++ b/src/users/dto/change-email.dto.ts @@ -0,0 +1,13 @@ +import { IsNotEmpty, IsEmail } from 'class-validator' + +export class ChangeEmailDto { + @IsNotEmpty() + @IsEmail() + email: string + + @IsNotEmpty() + verifyCode: string + + @IsNotEmpty() + token: string +} diff --git a/src/users/me.controller.ts b/src/users/me.controller.ts index a7f211e..9d0db1b 100644 --- a/src/users/me.controller.ts +++ b/src/users/me.controller.ts @@ -7,6 +7,7 @@ import { Body, UseInterceptors, BadRequestException, + Query, } from '@nestjs/common' import { MeService } from './me.service' import { ApiTags, ApiOperation, ApiUnauthorizedResponse } from '@nestjs/swagger' @@ -19,6 +20,7 @@ import { DeleteUserDto } from './dto/delete-user.dto' import { ChangePassword } from './dto/change-password.dto' import { UpdateUserDto } from './dto/update-user.dto' import { TokenRefreshPayload } from './dto/token.dto' +import { ChangeEmailDto } from './dto/change-email.dto' @Controller('api/users/me') @ApiTags('Me') @@ -65,7 +67,7 @@ export class MeController { return this.meService.deleteUser(userData, userId) } - @Patch('?field=password') + @Patch('password') @NeedAuth() @ApiOperation({ summary: '修改密码' }) @UseInterceptors(PasswordInterceptor) @@ -76,12 +78,17 @@ export class MeController { return this.meService.changePassword(payload, userId) } - @Patch('?field=email') + @Patch('email') @NeedAuth() - @ApiOperation({ summary: '修改邮箱(TODO)' }) + @ApiOperation({ summary: '修改邮箱' }) @UseInterceptors(PasswordInterceptor) - async updateEmail(@Body() payload: unknown) { - return '修改邮箱' + async changeEmail( + @Query() query, + @Body() payload: ChangeEmailDto, + @User('userId') userId: string, + ): Promise { + console.log(query) + return this.meService.changeEmail(payload, userId) } @Put('token') diff --git a/src/users/me.service.ts b/src/users/me.service.ts index d138b06..9074384 100644 --- a/src/users/me.service.ts +++ b/src/users/me.service.ts @@ -13,6 +13,7 @@ import { EmailService } from 'src/email/email.service' import { DeleteUserDto } from './dto/delete-user.dto' import { ChangePassword } from './dto/change-password.dto' import { TokenContnet } from './dto/token.dto' +import { ChangeEmailDto } from './dto/change-email.dto' @Injectable() export class MeService { @@ -62,6 +63,20 @@ export class MeService { }) } + async changeEmail(payload: ChangeEmailDto, userId: string) { + const { email, token, verifyCode } = payload + await this.emailService.verifyEmail({ + email, + token, + verifyCode, + scene: EmailScene.changeEmail, + }) + return this.prismaService.user.update({ + where: { id: userId }, + data: { email }, + }) + } + async updateAccessToken(refreshToken: string) { const { userId, iat } = this.jwtService.verify(refreshToken, { secret: this.secureConfig.jwt_refresh_secret, diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 8e4548b..527a6b9 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -23,7 +23,7 @@ export class UsersController { return this.usersService.loginByEmail(user.email, user.password) } - @Patch(':id/?field=password') + @Patch(':id/password') @ApiOperation({ summary: '找回密码' }) async forgetPassword( @Body() payload: ResetPassword,