63 lines
1.4 KiB
Go
Raw Normal View History

2024-08-04 01:14:59 +07:00
package customerauth
import (
2025-04-05 11:28:06 +08:00
auth2 "enaklo-pos-be/internal/handlers/request"
2025-04-26 12:23:12 +07:00
"enaklo-pos-be/internal/services/v2/auth"
2025-04-05 11:28:06 +08:00
"enaklo-pos-be/internal/services/v2/customer"
2024-08-04 01:14:59 +07:00
"net/http"
2024-09-03 23:44:57 +07:00
"strings"
2024-08-04 01:14:59 +07:00
"github.com/gin-gonic/gin"
2025-03-04 20:36:17 +07:00
"enaklo-pos-be/internal/common/errors"
"enaklo-pos-be/internal/handlers/response"
"enaklo-pos-be/internal/services"
2024-08-04 01:14:59 +07:00
)
type AuthHandler struct {
2025-04-26 12:23:12 +07:00
service auth.Service
2024-08-04 01:14:59 +07:00
userService services.User
2025-04-05 11:28:06 +08:00
customerSvc customer.Service
2024-08-04 01:14:59 +07:00
}
func (a *AuthHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
authRoute := group.Group("/auth")
authRoute.POST("/login", a.AuthLogin)
}
2025-04-26 12:23:12 +07:00
func NewAuthHandler(service auth.Service) *AuthHandler {
2024-08-04 01:14:59 +07:00
return &AuthHandler{
2025-04-26 12:23:12 +07:00
service: service,
2024-08-04 01:14:59 +07:00
}
}
func (h *AuthHandler) AuthLogin(c *gin.Context) {
2025-04-26 12:23:12 +07:00
ctx := auth2.GetMyContext(c)
2024-08-04 01:14:59 +07:00
var bodyParam auth2.LoginRequest
if err := c.ShouldBindJSON(&bodyParam); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
2024-09-03 23:44:57 +07:00
email := strings.ToLower(bodyParam.Email)
2025-04-26 12:23:12 +07:00
authUser, err := h.service.AuthCustomer(ctx, email, bodyParam.Password)
2024-08-04 01:14:59 +07:00
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,
})
}