From 8039a3571d9abba7f754de0143bafd908174b920 Mon Sep 17 00:00:00 2001 From: tone <3341154833@qq.com> Date: Wed, 7 May 2025 16:06:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=99=BB=E5=BD=95=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tone-page-server/src/auth/auth.module.ts | 4 ++- tone-page-server/src/auth/auth.service.ts | 30 +++++++++++++++++-- .../src/verification/verification.module.ts | 3 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/tone-page-server/src/auth/auth.module.ts b/tone-page-server/src/auth/auth.module.ts index 5a37392..d061946 100644 --- a/tone-page-server/src/auth/auth.module.ts +++ b/tone-page-server/src/auth/auth.module.ts @@ -8,6 +8,7 @@ import { UserSession } from 'src/user/entities/user-session.entity'; import { PassportModule } from '@nestjs/passport'; import { JwtStrategy } from './strategies/jwt.strategy'; import { ConfigModule, ConfigService } from '@nestjs/config'; +import { VerificationModule } from 'src/verification/verification.module'; @Module({ imports: [ @@ -24,7 +25,8 @@ import { ConfigModule, ConfigService } from '@nestjs/config'; expiresIn: configService.get('JWT_EXPIRES_IN', '1d'), }, }) - }) + }), + VerificationModule, ], controllers: [AuthController], providers: [ diff --git a/tone-page-server/src/auth/auth.service.ts b/tone-page-server/src/auth/auth.service.ts index 907b0b6..75262d0 100644 --- a/tone-page-server/src/auth/auth.service.ts +++ b/tone-page-server/src/auth/auth.service.ts @@ -6,6 +6,7 @@ import { User } from 'src/user/entities/user.entity'; import { JwtService } from '@nestjs/jwt'; import { UserSessionService } from 'src/user/services/user-session.service'; import { v4 as uuidv4 } from 'uuid'; +import { VerificationService } from 'src/verification/verification.service'; @Injectable() export class AuthService { @@ -14,6 +15,7 @@ export class AuthService { private readonly userService: UserService, private readonly jwtService: JwtService, private readonly userSessionService: UserSessionService, + private readonly verificationService: VerificationService, ) { } async loginWithPassword(loginDto: LoginDto) { @@ -44,7 +46,19 @@ export class AuthService { async loginWithPhone(loginDto: LoginDto) { const { phone, code } = loginDto; // 先判断验证码是否正确 - // TODO + const isValid = this.verificationService.verifyPhoneCode(phone, code, 'login'); + switch (isValid) { + case 0: + break; + case -1: + throw new BadRequestException('验证码已过期'); + case -2: + throw new BadRequestException('验证码错误'); + case -3: + throw new BadRequestException('验证码已失效'); + default: + throw new BadRequestException('验证码错误'); + } // 判断用户是否存在,若不存在则进行注册 let user = await this.userService.findOne({ phone }); @@ -66,7 +80,19 @@ export class AuthService { async loginWithEmail(loginDto: LoginDto) { const { email, code } = loginDto; // 先判断验证码是否正确 - // TODO + const isValid = this.verificationService.verifyEmailCode(email, code, 'login'); + switch (isValid) { + case 0: + break; + case -1: + throw new BadRequestException('验证码已过期,请重新获取'); + case -2: + throw new BadRequestException('验证码错误'); + case -3: + throw new BadRequestException('验证码已失效,请重新获取'); + default: + throw new BadRequestException('验证码错误,请稍后再试'); + } // 判断用户是否存在,若不存在则进行注册 let user = await this.userService.findOne({ email }); diff --git a/tone-page-server/src/verification/verification.module.ts b/tone-page-server/src/verification/verification.module.ts index c8fd9d3..54738ca 100644 --- a/tone-page-server/src/verification/verification.module.ts +++ b/tone-page-server/src/verification/verification.module.ts @@ -4,6 +4,7 @@ import { VerificationService } from './verification.service'; @Module({ controllers: [VerificationController], - providers: [VerificationService] + providers: [VerificationService], + exports: [VerificationService], }) export class VerificationModule {}