mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-27 21:20:57 +00:00
64 lines
1.4 KiB
TypeScript
64 lines
1.4 KiB
TypeScript
// otp.controller.ts
|
|
|
|
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
Post,
|
|
} from "@nestjs/common";
|
|
|
|
|
|
import { OtpService, OtpTarget } from "./otp.service";
|
|
import { Public } from "@edr/api-common";
|
|
|
|
// Exactly one of phone/email must be present per request — the channel the
|
|
// code is sent through / checked against.
|
|
function toTarget(phone?: string, email?: string): OtpTarget {
|
|
if (email) return { email };
|
|
if (phone) return { phone };
|
|
throw new BadRequestException("phone or email is required");
|
|
}
|
|
|
|
@Controller("otp")
|
|
@Public()
|
|
export class OtpController {
|
|
constructor(
|
|
private readonly otpService: OtpService
|
|
) {}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Send OTP
|
|
// ---------------------------------------------------------------------------
|
|
|
|
@Post("send")
|
|
async sendOtp(
|
|
@Body("phone")
|
|
phone?: string,
|
|
|
|
@Body("email")
|
|
email?: string
|
|
) {
|
|
return this.otpService.sendOtp(toTarget(phone, email));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Verify OTP
|
|
// ---------------------------------------------------------------------------
|
|
|
|
@Post("verify")
|
|
async verifyOtp(
|
|
@Body("phone")
|
|
phone: string | undefined,
|
|
|
|
@Body("email")
|
|
email: string | undefined,
|
|
|
|
@Body("otp")
|
|
otp: string
|
|
) {
|
|
return this.otpService.verifyOtp(
|
|
toTarget(phone, email),
|
|
otp
|
|
);
|
|
}
|
|
} |