138 lines
3.1 KiB
Go
138 lines
3.1 KiB
Go
package customerauth
|
|
|
|
import (
|
|
"enaklo-pos-be/internal/entity"
|
|
auth2 "enaklo-pos-be/internal/handlers/request"
|
|
"enaklo-pos-be/internal/services/member"
|
|
"enaklo-pos-be/internal/services/v2/auth"
|
|
"github.com/go-playground/validator/v10"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"enaklo-pos-be/internal/common/errors"
|
|
"enaklo-pos-be/internal/handlers/response"
|
|
)
|
|
|
|
type AuthHandler struct {
|
|
service auth.Service
|
|
memberSvc member.RegistrationService
|
|
}
|
|
|
|
func (a *AuthHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
authRoute := group.Group("/auth")
|
|
authRoute.POST("/login", a.AuthLogin)
|
|
authRoute.POST("/register", a.Registration)
|
|
authRoute.POST("/verify-otp", a.VerifyOTP)
|
|
}
|
|
|
|
func NewAuthHandler(service auth.Service, memberSvc member.RegistrationService) *AuthHandler {
|
|
return &AuthHandler{
|
|
service: service,
|
|
memberSvc: memberSvc,
|
|
}
|
|
}
|
|
|
|
func (h *AuthHandler) AuthLogin(c *gin.Context) {
|
|
ctx := auth2.GetMyContext(c)
|
|
|
|
var bodyParam auth2.LoginRequest
|
|
if err := c.ShouldBindJSON(&bodyParam); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
email := strings.ToLower(bodyParam.Email)
|
|
authUser, err := h.service.AuthCustomer(ctx, email, bodyParam.Password)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
resp := response.LoginResponseCustoemr{
|
|
ID: authUser.ID,
|
|
Token: authUser.Token,
|
|
Name: authUser.Name,
|
|
ResetPassword: authUser.ResetPassword,
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Message: "Login Success",
|
|
Data: resp,
|
|
})
|
|
}
|
|
|
|
func (h *AuthHandler) Registration(c *gin.Context) {
|
|
ctx := auth2.GetMyContext(c)
|
|
userID := ctx.RequestedBy()
|
|
|
|
var req auth2.InitiateRegistrationRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
validate := validator.New()
|
|
if err := validate.Struct(req); err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
birthDate, err := req.GetBirthdate()
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
memberReq := &entity.MemberRegistrationRequest{
|
|
Name: req.Name,
|
|
Email: req.Email,
|
|
Phone: req.Phone,
|
|
BirthDate: birthDate,
|
|
CashierID: userID,
|
|
Password: req.Password,
|
|
}
|
|
|
|
result, err := h.memberSvc.InitiateRegistration(ctx, memberReq)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: response.MapToMemberRegistrationResponse(result),
|
|
})
|
|
}
|
|
|
|
func (h *AuthHandler) VerifyOTP(c *gin.Context) {
|
|
ctx := auth2.GetMyContext(c)
|
|
|
|
var req auth2.VerifyOTPRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
validate := validator.New()
|
|
if err := validate.Struct(req); err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
result, err := h.memberSvc.VerifyOTP(ctx, req.Token, req.OTP)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: response.MapToMemberVerificationResponse(result.Auth),
|
|
})
|
|
}
|