完成后端登录dto验证

This commit is contained in:
2025-05-06 22:52:51 +08:00
parent cab4fdb6e1
commit 94cc8feda8
30 changed files with 7027 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthController } from './auth.controller';
describe('AuthController', () => {
let controller: AuthController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
}).compile();
controller = module.get<AuthController>(AuthController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@@ -0,0 +1,25 @@
import { BadRequestException, Body, Controller, Post } from '@nestjs/common';
import { LoginDto } from './dto/login.dto';
import { AuthService } from './auth.service';
@Controller('auth')
export class AuthController {
constructor(
private readonly authService: AuthService,
) { }
@Post('login')
async login(@Body() loginDto: LoginDto) {
switch (loginDto.type) {
case 'password':
return this.authService.loginWithPassword(loginDto);
case 'phone':
return this.authService.loginWithPhone(loginDto);
case 'email':
return this.authService.loginWithEmail(loginDto);
default:
throw new BadRequestException('Invalid login type');
}
}
}

View File

@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
@Module({
controllers: [AuthController],
providers: [AuthService]
})
export class AuthModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
describe('AuthService', () => {
let service: AuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
}).compile();
service = module.get<AuthService>(AuthService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -0,0 +1,32 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { LoginDto } from './dto/login.dto';
@Injectable()
export class AuthService {
async loginWithPassword(loginDto: LoginDto) {
const { account, password } = loginDto;
if (!account || !password) {
throw new BadRequestException('account and password are required');
}
return { message: 'Logged in with password', data: loginDto };
}
async loginWithPhone(loginDto: LoginDto) {
const { phone, code } = loginDto;
if (!phone || !code) {
throw new BadRequestException('Phone number and code are required');
}
return { message: 'Logged in with phone', data: loginDto };
}
async loginWithEmail(loginDto: LoginDto) {
const { email, code } = loginDto;
if (!email || !code) {
throw new BadRequestException('Email and code are required');
}
return { message: 'Logged in with email', data: loginDto };
}
}

View File

@@ -0,0 +1,31 @@
import { IsEnum, IsString, Length, ValidateIf } from 'class-validator';
export class LoginDto {
@IsEnum(['password', 'phone', 'email'], { message: '请求类型错误' })
type: 'password' | 'phone' | 'email';
@ValidateIf(o => o.type === 'password')
@IsString({ message: '账户必须输入' })
@Length(1, 254, { message: '账户长度错误' })// 用户名、邮箱、手机号
account?: string;
@ValidateIf(o => o.type === 'password')
@IsString({ message: '密码必须输入' })
@Length(6, 32, { message: '密码长度错误' })// 6-32位
password?: string;
@ValidateIf(o => o.type === 'phone')
@IsString({ message: '手机号必须输入' })
@Length(11, 11, { message: '手机号长度错误' })// 中国大陆11位数字
phone?: string;
@ValidateIf(o => o.type === 'email')
@IsString({ message: '邮箱必须输入' })
@Length(6, 254, { message: '邮箱长度错误' })// RFC 5321
email?: string;
@ValidateIf(o => o.type === 'phone' || o.type === 'email')
@IsString({ message: '验证码必须输入' })
@Length(6, 6, { message: '验证码长度错误' })// 6位数字
code?: string;
}