DanzApp-BE-Test / controllers /otp_verification_controller.go
lifedebugger's picture
Deploy files from GitHub repository
4a66f20
package controllers
import (
"abdanhafidz.com/go-boilerplate/models/dto"
"abdanhafidz.com/go-boilerplate/services"
"github.com/gin-gonic/gin"
)
type OTPVerificationController interface {
RequestOTP(ctx *gin.Context)
VerifyOTP(ctx *gin.Context)
}
type otpVerificationController struct {
otpService services.OTPVerificationService
}
func NewOTPVerificationController(otpService services.OTPVerificationService) OTPVerificationController {
return &otpVerificationController{
otpService: otpService,
}
}
// DTOs moved to models/dto/otp_dto.go
// RequestOTP godoc
// @Summary Request OTP
// @Description Trigger an OTP code via WhatsApp for phone number verification.
// @Tags Auth
// @Accept json
// @Produce json
// @Param request body dto.OTPRequestInput true "OTP Request Data"
// @Success 200 {object} dto.OTPRequestResponse
// @Failure 400 {object} dto.ErrorResponse
// @Router /api/v1/auth/otp/request [post]
func (c *otpVerificationController) RequestOTP(ctx *gin.Context) {
req, err := RequestJSON[dto.OTPRequestInput](ctx)
if err != nil {
return
}
res, err := c.otpService.RequestOTP(ctx.Request.Context(), req)
if err != nil {
ResponseJSON(ctx, "", dto.OTPRequestResponse{}, err)
return
}
ResponseJSON(ctx, "OTP sent successfully via WhatsApp", res, nil)
}
// VerifyOTP godoc
// @Summary Verify OTP
// @Description Validate the OTP code provided by the user and return authentication token.
// @Tags Auth
// @Accept json
// @Produce json
// @Param request body dto.OTPVerifyInput true "OTP Verification Data"
// @Success 200 {object} dto.OTPVerifyResponse
// @Failure 400 {object} dto.ErrorResponse
// @Router /api/v1/auth/otp/verify [post]
func (c *otpVerificationController) VerifyOTP(ctx *gin.Context) {
req, err := RequestJSON[dto.OTPVerifyInput](ctx)
if err != nil {
return
}
res, err := c.otpService.VerifyOTP(ctx.Request.Context(), req)
if err != nil {
ResponseJSON(ctx, "", dto.OTPVerifyResponse{}, err)
return
}
ResponseJSON(ctx, "OTP verified successfully", res, nil)
}