aditya.siregar 06d7b4764f update
2025-04-26 12:23:12 +07:00

63 lines
1.4 KiB
Go

package customerauth
import (
auth2 "enaklo-pos-be/internal/handlers/request"
"enaklo-pos-be/internal/services/v2/auth"
"enaklo-pos-be/internal/services/v2/customer"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"enaklo-pos-be/internal/common/errors"
"enaklo-pos-be/internal/handlers/response"
"enaklo-pos-be/internal/services"
)
type AuthHandler struct {
service auth.Service
userService services.User
customerSvc customer.Service
}
func (a *AuthHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
authRoute := group.Group("/auth")
authRoute.POST("/login", a.AuthLogin)
}
func NewAuthHandler(service auth.Service) *AuthHandler {
return &AuthHandler{
service: service,
}
}
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,
})
}