feat: refactor auth package and add get users for staff
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
package authhttp
|
||||
|
||||
import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("auth-api",
|
||||
fx.Invoke(
|
||||
LoginStaff,
|
||||
LoginUser,
|
||||
RegisterUser,
|
||||
RegisterStaff,
|
||||
UpdateStaff,
|
||||
GetStaffProfile,
|
||||
GetUserProfile,
|
||||
),
|
||||
)
|
||||
@@ -1,12 +1,13 @@
|
||||
package internalhttp
|
||||
|
||||
import (
|
||||
authhttp "legalgo-BE-go/internal/api/http/auth"
|
||||
categoryhttp "legalgo-BE-go/internal/api/http/category"
|
||||
newshttp "legalgo-BE-go/internal/api/http/news"
|
||||
osshttp "legalgo-BE-go/internal/api/http/oss"
|
||||
staffhttp "legalgo-BE-go/internal/api/http/staffhttp"
|
||||
subscribeplanhttp "legalgo-BE-go/internal/api/http/subscribe_plan"
|
||||
taghttp "legalgo-BE-go/internal/api/http/tag"
|
||||
userhttp "legalgo-BE-go/internal/api/http/user"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
@@ -21,12 +22,13 @@ var Module = fx.Module("router",
|
||||
initRouter,
|
||||
validator.New,
|
||||
),
|
||||
authhttp.Module,
|
||||
staffhttp.Module,
|
||||
subscribeplanhttp.Module,
|
||||
taghttp.Module,
|
||||
categoryhttp.Module,
|
||||
newshttp.Module,
|
||||
osshttp.Module,
|
||||
userhttp.Module,
|
||||
)
|
||||
|
||||
func initRouter() chi.Router {
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package staffhttp
|
||||
|
||||
import (
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
authsvc "legalgo-BE-go/internal/services/auth"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func GetUsers(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
) {
|
||||
router.With(authmiddleware.Authorize()).Get("/staff/users", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
staffDetail, err := utils.GetTokenDetail(r)
|
||||
if err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to get staff token",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if staffDetail.Role != "staff" {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrUnauthorized.Code,
|
||||
response.ErrUnauthorized.HttpCode,
|
||||
"unauthorized",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
users, err := authSvc.GetUsers()
|
||||
if err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to get users",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, users)
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package authhttp
|
||||
package staffhttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func LoginStaff(
|
||||
func Login(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
validate *validator.Validate,
|
||||
@@ -0,0 +1,13 @@
|
||||
package staffhttp
|
||||
|
||||
import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("auth-api",
|
||||
fx.Invoke(
|
||||
Login,
|
||||
Register,
|
||||
Update,
|
||||
GetProfile,
|
||||
GetUsers,
|
||||
),
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package authhttp
|
||||
package staffhttp
|
||||
|
||||
import (
|
||||
authsvc "legalgo-BE-go/internal/services/auth"
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func GetStaffProfile(
|
||||
func GetProfile(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
) {
|
||||
@@ -44,39 +44,3 @@ func GetStaffProfile(
|
||||
response.RespondJsonSuccess(ctx, w, staffProfile)
|
||||
})
|
||||
}
|
||||
|
||||
func GetUserProfile(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
) {
|
||||
router.Get("/user/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
destructedToken, err := utils.GetTokenDetail(r)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
userProfile, err := authSvc.GetUserProfile(destructedToken.Email)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, userProfile)
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package authhttp
|
||||
package staffhttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func RegisterStaff(
|
||||
func Register(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
authSvc authsvc.Auth,
|
||||
@@ -1,4 +1,4 @@
|
||||
package authhttp
|
||||
package staffhttp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func UpdateStaff(
|
||||
func Update(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
) {
|
||||
@@ -1,4 +1,4 @@
|
||||
package authhttp
|
||||
package userhttp
|
||||
|
||||
import (
|
||||
responsedomain "legalgo-BE-go/internal/domain/reponse"
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func LoginUser(
|
||||
func Login(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
validate *validator.Validate,
|
||||
@@ -0,0 +1,9 @@
|
||||
package userhttp
|
||||
|
||||
import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("user-http", fx.Invoke(
|
||||
Register,
|
||||
Login,
|
||||
GetProfile,
|
||||
))
|
||||
@@ -0,0 +1,46 @@
|
||||
package userhttp
|
||||
|
||||
import (
|
||||
authsvc "legalgo-BE-go/internal/services/auth"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func GetProfile(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
) {
|
||||
router.Get("/user/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
destructedToken, err := utils.GetTokenDetail(r)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
userProfile, err := authSvc.GetUserProfile(destructedToken.Email)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, userProfile)
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package authhttp
|
||||
package userhttp
|
||||
|
||||
import (
|
||||
responsedomain "legalgo-BE-go/internal/domain/reponse"
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func RegisterUser(
|
||||
func Register(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
authSvc authsvc.Auth,
|
||||
Reference in New Issue
Block a user