123 lines
2.5 KiB
Go
123 lines
2.5 KiB
Go
package authhttp
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
|
|
authsvc "github.com/ardeman/project-legalgo-go/internal/services/auth"
|
|
"github.com/ardeman/project-legalgo-go/internal/utilities/response"
|
|
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
func LoginStaff(
|
|
router chi.Router,
|
|
authSvc authsvc.AuthIntf,
|
|
validate *validator.Validate,
|
|
) {
|
|
router.Post("/staff/login", func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var spec authdomain.LoginReq
|
|
|
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
|
response.ResponseWithErrorCode(
|
|
ctx,
|
|
w,
|
|
err,
|
|
response.ErrBadRequest.Code,
|
|
response.ErrBadRequest.HttpCode,
|
|
"failed to unmarshal request",
|
|
)
|
|
return
|
|
}
|
|
|
|
if err := validate.Struct(spec); err != nil {
|
|
response.ResponseWithErrorCode(
|
|
ctx,
|
|
w,
|
|
err,
|
|
response.ErrBadRequest.Code,
|
|
response.ErrBadRequest.HttpCode,
|
|
err.(validator.ValidationErrors).Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
token, err := authSvc.LoginAsStaff(spec)
|
|
if err != nil {
|
|
response.ResponseWithErrorCode(
|
|
ctx,
|
|
w,
|
|
err,
|
|
response.ErrBadRequest.Code,
|
|
response.ErrBadRequest.HttpCode,
|
|
err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
responsePayload := &authdomain.LoginResponse{
|
|
Token: token,
|
|
}
|
|
|
|
response.RespondJsonSuccess(ctx, w, responsePayload)
|
|
})
|
|
}
|
|
|
|
func LoginUser(
|
|
router chi.Router,
|
|
authSvc authsvc.AuthIntf,
|
|
validate *validator.Validate,
|
|
) {
|
|
router.Post("/user/login", func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var spec authdomain.LoginReq
|
|
|
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
|
response.ResponseWithErrorCode(
|
|
ctx,
|
|
w,
|
|
err,
|
|
response.ErrBadRequest.Code,
|
|
response.ErrBadRequest.HttpCode,
|
|
"failed to unmarshal request",
|
|
)
|
|
return
|
|
}
|
|
|
|
if err := validate.Struct(spec); err != nil {
|
|
response.ResponseWithErrorCode(
|
|
ctx,
|
|
w,
|
|
err,
|
|
response.ErrBadRequest.Code,
|
|
response.ErrBadRequest.HttpCode,
|
|
err.(validator.ValidationErrors).Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
token, err := authSvc.LoginAsUser(spec)
|
|
if err != nil {
|
|
response.ResponseWithErrorCode(
|
|
ctx,
|
|
w,
|
|
err,
|
|
response.ErrBadRequest.Code,
|
|
response.ErrBadRequest.HttpCode,
|
|
err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
responsePayload := &authdomain.LoginResponse{
|
|
Token: token,
|
|
}
|
|
|
|
response.RespondJsonSuccess(ctx, w, responsePayload)
|
|
})
|
|
}
|