changeEmail
This commit is contained in:
parent
5bbefbcf4a
commit
a00990b6d6
@ -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]},验证码为 <strong>${verifyCode}</strong>,30分钟内有效`,
|
||||
})
|
||||
return { registerToken, userId: user.id }
|
||||
return { token, userId: user?.id }
|
||||
}
|
||||
|
||||
// TODO: 做成守卫?
|
||||
|
13
src/users/dto/change-email.dto.ts
Normal file
13
src/users/dto/change-email.dto.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { IsNotEmpty, IsEmail } from 'class-validator'
|
||||
|
||||
export class ChangeEmailDto {
|
||||
@IsNotEmpty()
|
||||
@IsEmail()
|
||||
email: string
|
||||
|
||||
@IsNotEmpty()
|
||||
verifyCode: string
|
||||
|
||||
@IsNotEmpty()
|
||||
token: string
|
||||
}
|
@ -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<UserEntity> {
|
||||
console.log(query)
|
||||
return this.meService.changeEmail(payload, userId)
|
||||
}
|
||||
|
||||
@Put('token')
|
||||
|
@ -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<TokenContnet>(refreshToken, {
|
||||
secret: this.secureConfig.jwt_refresh_secret,
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user