实现登录验证
This commit is contained in:
@@ -8,6 +8,7 @@ import { UserSession } from 'src/user/entities/user-session.entity';
|
|||||||
import { PassportModule } from '@nestjs/passport';
|
import { PassportModule } from '@nestjs/passport';
|
||||||
import { JwtStrategy } from './strategies/jwt.strategy';
|
import { JwtStrategy } from './strategies/jwt.strategy';
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
|
import { VerificationModule } from 'src/verification/verification.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -24,7 +25,8 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
|
|||||||
expiresIn: configService.get<string>('JWT_EXPIRES_IN', '1d'),
|
expiresIn: configService.get<string>('JWT_EXPIRES_IN', '1d'),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
}),
|
||||||
|
VerificationModule,
|
||||||
],
|
],
|
||||||
controllers: [AuthController],
|
controllers: [AuthController],
|
||||||
providers: [
|
providers: [
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { User } from 'src/user/entities/user.entity';
|
|||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { UserSessionService } from 'src/user/services/user-session.service';
|
import { UserSessionService } from 'src/user/services/user-session.service';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
import { VerificationService } from 'src/verification/verification.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
@@ -14,6 +15,7 @@ export class AuthService {
|
|||||||
private readonly userService: UserService,
|
private readonly userService: UserService,
|
||||||
private readonly jwtService: JwtService,
|
private readonly jwtService: JwtService,
|
||||||
private readonly userSessionService: UserSessionService,
|
private readonly userSessionService: UserSessionService,
|
||||||
|
private readonly verificationService: VerificationService,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async loginWithPassword(loginDto: LoginDto) {
|
async loginWithPassword(loginDto: LoginDto) {
|
||||||
@@ -44,7 +46,19 @@ export class AuthService {
|
|||||||
async loginWithPhone(loginDto: LoginDto) {
|
async loginWithPhone(loginDto: LoginDto) {
|
||||||
const { phone, code } = 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 });
|
let user = await this.userService.findOne({ phone });
|
||||||
@@ -66,7 +80,19 @@ export class AuthService {
|
|||||||
async loginWithEmail(loginDto: LoginDto) {
|
async loginWithEmail(loginDto: LoginDto) {
|
||||||
const { email, code } = 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 });
|
let user = await this.userService.findOne({ email });
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { VerificationService } from './verification.service';
|
|||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [VerificationController],
|
controllers: [VerificationController],
|
||||||
providers: [VerificationService]
|
providers: [VerificationService],
|
||||||
|
exports: [VerificationService],
|
||||||
})
|
})
|
||||||
export class VerificationModule {}
|
export class VerificationModule {}
|
||||||
|
|||||||
Reference in New Issue
Block a user