File size: 2,235 Bytes
92bfa2a
 
 
d8cdd09
92bfa2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8cdd09
92bfa2a
12690b4
 
 
 
 
 
 
 
 
 
92bfa2a
4a66f20
 
 
 
d8cdd09
 
 
 
 
 
92bfa2a
 
12690b4
 
 
 
 
 
 
 
 
 
92bfa2a
4a66f20
 
 
 
d8cdd09
 
 
 
 
 
92bfa2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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)
}