feat: login service and implement database storage
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package authhttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
domain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
|
||||
serviceauth "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 serviceauth.LoginStaffIntf,
|
||||
validate *validator.Validate,
|
||||
) {
|
||||
router.Post("/staff/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var request domain.StaffLoginReq
|
||||
|
||||
if err := utils.UnmarshalBody(r, &request); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(request); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := authSvc.LoginAsStaff(request.Email)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
responsePayload := &domain.StaffLoginResponse{
|
||||
Token: token,
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, responsePayload)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package authhttp
|
||||
|
||||
import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("auth-api",
|
||||
fx.Invoke(
|
||||
LoginStaff,
|
||||
),
|
||||
)
|
||||
@@ -1,15 +1,21 @@
|
||||
package internalhttp
|
||||
|
||||
import (
|
||||
authhttp "github.com/ardeman/project-legalgo-go/internal/api/http/auth"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"go.uber.org/fx"
|
||||
|
||||
chimware "github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
var Module = fx.Module("router",
|
||||
fx.Provide(initRouter),
|
||||
fx.Provide(
|
||||
initRouter,
|
||||
validator.New,
|
||||
),
|
||||
authhttp.Module,
|
||||
)
|
||||
|
||||
func initRouter() chi.Router {
|
||||
|
||||
Reference in New Issue
Block a user