完成后端登录dto验证
This commit is contained in:
18
tone-page-server/src/auth/auth.controller.spec.ts
Normal file
18
tone-page-server/src/auth/auth.controller.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
25
tone-page-server/src/auth/auth.controller.ts
Normal file
25
tone-page-server/src/auth/auth.controller.ts
Normal 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');
|
||||
}
|
||||
}
|
||||
}
|
||||
9
tone-page-server/src/auth/auth.module.ts
Normal file
9
tone-page-server/src/auth/auth.module.ts
Normal 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 {}
|
||||
18
tone-page-server/src/auth/auth.service.spec.ts
Normal file
18
tone-page-server/src/auth/auth.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
32
tone-page-server/src/auth/auth.service.ts
Normal file
32
tone-page-server/src/auth/auth.service.ts
Normal 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 };
|
||||
}
|
||||
}
|
||||
31
tone-page-server/src/auth/dto/login.dto.ts
Normal file
31
tone-page-server/src/auth/dto/login.dto.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user