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 { JwtService } from '@nestjs/jwt'
|
||||||
import { PrismaService } from 'nestjs-prisma'
|
import { PrismaService } from 'nestjs-prisma'
|
||||||
import { EmailSendDto, EmailScene } from 'src/email/dto/email.dto'
|
import { EmailSendDto, EmailScene } from 'src/email/dto/email.dto'
|
||||||
|
import { UserEntity } from 'src/users/entities/user.entity'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class EmailService {
|
export class EmailService {
|
||||||
@ -28,7 +29,7 @@ export class EmailService {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
async sendEmailToken(email: string, scene: EmailScene) {
|
async sendEmailToken(email: string, scene: EmailScene) {
|
||||||
const user = await this.prismaService.user.findUnique({
|
const user: UserEntity | null = await this.prismaService.user.findUnique({
|
||||||
where: { email },
|
where: { email },
|
||||||
})
|
})
|
||||||
switch (scene) {
|
switch (scene) {
|
||||||
@ -47,7 +48,7 @@ export class EmailService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const verifyCode = this.generateVerifyCode()
|
const verifyCode = this.generateVerifyCode()
|
||||||
const registerToken = this.jwtService.sign(
|
const token = this.jwtService.sign(
|
||||||
{ email, scene },
|
{ email, scene },
|
||||||
{ secret: this.getEmailJwtSecret(verifyCode, scene) },
|
{ secret: this.getEmailJwtSecret(verifyCode, scene) },
|
||||||
)
|
)
|
||||||
@ -56,7 +57,7 @@ export class EmailService {
|
|||||||
subject: `【qiuxu.site】${this.subjectMap[scene]}`,
|
subject: `【qiuxu.site】${this.subjectMap[scene]}`,
|
||||||
html: `您正在qiuxu.site${this.subjectMap[scene]},验证码为 <strong>${verifyCode}</strong>,30分钟内有效`,
|
html: `您正在qiuxu.site${this.subjectMap[scene]},验证码为 <strong>${verifyCode}</strong>,30分钟内有效`,
|
||||||
})
|
})
|
||||||
return { registerToken, userId: user.id }
|
return { token, userId: user?.id }
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: 做成守卫?
|
// 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,
|
Body,
|
||||||
UseInterceptors,
|
UseInterceptors,
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
|
Query,
|
||||||
} from '@nestjs/common'
|
} from '@nestjs/common'
|
||||||
import { MeService } from './me.service'
|
import { MeService } from './me.service'
|
||||||
import { ApiTags, ApiOperation, ApiUnauthorizedResponse } from '@nestjs/swagger'
|
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 { ChangePassword } from './dto/change-password.dto'
|
||||||
import { UpdateUserDto } from './dto/update-user.dto'
|
import { UpdateUserDto } from './dto/update-user.dto'
|
||||||
import { TokenRefreshPayload } from './dto/token.dto'
|
import { TokenRefreshPayload } from './dto/token.dto'
|
||||||
|
import { ChangeEmailDto } from './dto/change-email.dto'
|
||||||
|
|
||||||
@Controller('api/users/me')
|
@Controller('api/users/me')
|
||||||
@ApiTags('Me')
|
@ApiTags('Me')
|
||||||
@ -65,7 +67,7 @@ export class MeController {
|
|||||||
return this.meService.deleteUser(userData, userId)
|
return this.meService.deleteUser(userData, userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('?field=password')
|
@Patch('password')
|
||||||
@NeedAuth()
|
@NeedAuth()
|
||||||
@ApiOperation({ summary: '修改密码' })
|
@ApiOperation({ summary: '修改密码' })
|
||||||
@UseInterceptors(PasswordInterceptor)
|
@UseInterceptors(PasswordInterceptor)
|
||||||
@ -76,12 +78,17 @@ export class MeController {
|
|||||||
return this.meService.changePassword(payload, userId)
|
return this.meService.changePassword(payload, userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('?field=email')
|
@Patch('email')
|
||||||
@NeedAuth()
|
@NeedAuth()
|
||||||
@ApiOperation({ summary: '修改邮箱(TODO)' })
|
@ApiOperation({ summary: '修改邮箱' })
|
||||||
@UseInterceptors(PasswordInterceptor)
|
@UseInterceptors(PasswordInterceptor)
|
||||||
async updateEmail(@Body() payload: unknown) {
|
async changeEmail(
|
||||||
return '修改邮箱'
|
@Query() query,
|
||||||
|
@Body() payload: ChangeEmailDto,
|
||||||
|
@User('userId') userId: string,
|
||||||
|
): Promise<UserEntity> {
|
||||||
|
console.log(query)
|
||||||
|
return this.meService.changeEmail(payload, userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put('token')
|
@Put('token')
|
||||||
|
@ -13,6 +13,7 @@ import { EmailService } from 'src/email/email.service'
|
|||||||
import { DeleteUserDto } from './dto/delete-user.dto'
|
import { DeleteUserDto } from './dto/delete-user.dto'
|
||||||
import { ChangePassword } from './dto/change-password.dto'
|
import { ChangePassword } from './dto/change-password.dto'
|
||||||
import { TokenContnet } from './dto/token.dto'
|
import { TokenContnet } from './dto/token.dto'
|
||||||
|
import { ChangeEmailDto } from './dto/change-email.dto'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class MeService {
|
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) {
|
async updateAccessToken(refreshToken: string) {
|
||||||
const { userId, iat } = this.jwtService.verify<TokenContnet>(refreshToken, {
|
const { userId, iat } = this.jwtService.verify<TokenContnet>(refreshToken, {
|
||||||
secret: this.secureConfig.jwt_refresh_secret,
|
secret: this.secureConfig.jwt_refresh_secret,
|
||||||
|
@ -23,7 +23,7 @@ export class UsersController {
|
|||||||
return this.usersService.loginByEmail(user.email, user.password)
|
return this.usersService.loginByEmail(user.email, user.password)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id/?field=password')
|
@Patch(':id/password')
|
||||||
@ApiOperation({ summary: '找回密码' })
|
@ApiOperation({ summary: '找回密码' })
|
||||||
async forgetPassword(
|
async forgetPassword(
|
||||||
@Body() payload: ResetPassword,
|
@Body() payload: ResetPassword,
|
||||||
|
Loading…
Reference in New Issue
Block a user