2025-03-07 17:39:26 +08:00
|
|
|
package staffhttp
|
2025-02-23 22:34:26 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2025-03-05 21:21:44 +08:00
|
|
|
responsedomain "legalgo-BE-go/internal/domain/reponse"
|
|
|
|
|
staffdomain "legalgo-BE-go/internal/domain/staff"
|
2025-03-25 18:01:14 +08:00
|
|
|
staffsvc "legalgo-BE-go/internal/services/staff"
|
2025-02-27 07:25:25 +08:00
|
|
|
"legalgo-BE-go/internal/utilities/response"
|
|
|
|
|
"legalgo-BE-go/internal/utilities/utils"
|
|
|
|
|
|
2025-02-23 22:34:26 +08:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
2025-03-05 21:21:44 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
2025-02-23 22:34:26 +08:00
|
|
|
)
|
|
|
|
|
|
2025-03-07 17:39:26 +08:00
|
|
|
func Login(
|
2025-02-23 22:34:26 +08:00
|
|
|
router chi.Router,
|
2025-03-14 12:41:11 +08:00
|
|
|
authSvc staffsvc.Auth,
|
2025-02-23 22:34:26 +08:00
|
|
|
validate *validator.Validate,
|
2025-03-05 21:21:44 +08:00
|
|
|
rdb *redis.Client,
|
2025-02-23 22:34:26 +08:00
|
|
|
) {
|
|
|
|
|
router.Post("/staff/login", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
2025-03-05 21:21:44 +08:00
|
|
|
var spec staffdomain.StaffLogin
|
2025-02-23 22:34:26 +08:00
|
|
|
|
2025-02-24 16:48:20 +08:00
|
|
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
2025-02-23 22:34:26 +08:00
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
"failed to unmarshal request",
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 16:48:20 +08:00
|
|
|
if err := validate.Struct(spec); err != nil {
|
2025-02-23 22:34:26 +08:00
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.(validator.ValidationErrors).Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 12:41:11 +08:00
|
|
|
token, err := authSvc.Login(spec)
|
2025-02-23 22:34:26 +08:00
|
|
|
if err != nil {
|
|
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 21:21:44 +08:00
|
|
|
if err := utils.StoreTokenRedis(ctx, rdb, token, spec.Email); err != nil {
|
2025-02-24 16:48:20 +08:00
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 21:21:44 +08:00
|
|
|
responsePayload := &responsedomain.Auth{
|
2025-02-23 22:34:26 +08:00
|
|
|
Token: token,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.RespondJsonSuccess(ctx, w, responsePayload)
|
|
|
|
|
})
|
|
|
|
|
}
|