rwadurian/backend/services/auth-service/src/api/controllers/sms.controller.ts

64 lines
1.4 KiB
TypeScript

import {
Controller,
Post,
Body,
HttpCode,
HttpStatus,
UseGuards,
} from '@nestjs/common';
import { ThrottlerGuard } from '@nestjs/throttler';
import { SmsService } from '@/application/services';
import { SmsVerificationType } from '@/domain';
class SendSmsDto {
phone: string;
type: 'REGISTER' | 'LOGIN' | 'RESET_PASSWORD' | 'CHANGE_PHONE';
}
class VerifySmsDto {
phone: string;
code: string;
type: 'REGISTER' | 'LOGIN' | 'RESET_PASSWORD' | 'CHANGE_PHONE';
}
@Controller('auth/sms')
@UseGuards(ThrottlerGuard)
export class SmsController {
constructor(private readonly smsService: SmsService) {}
/**
* 发送验证码
* POST /sms/send
*/
@Post('send')
@HttpCode(HttpStatus.OK)
async sendCode(
@Body() dto: SendSmsDto,
): Promise<{ success: boolean; data: { expiresIn: number } }> {
const result = await this.smsService.sendCode({
phone: dto.phone,
type: dto.type as SmsVerificationType,
});
return { success: true, data: result };
}
/**
* 验证验证码
* POST /sms/verify
*/
@Post('verify')
@HttpCode(HttpStatus.OK)
async verifyCode(
@Body() dto: VerifySmsDto,
): Promise<{ success: boolean; data: { valid: boolean } }> {
const valid = await this.smsService.verifyCode({
phone: dto.phone,
code: dto.code,
type: dto.type as SmsVerificationType,
});
return { success: true, data: { valid } };
}
}