feat: CR login, register, subscribe
This commit is contained in:
@@ -3,8 +3,8 @@ 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"
|
||||
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"
|
||||
@@ -13,15 +13,15 @@ import (
|
||||
|
||||
func LoginStaff(
|
||||
router chi.Router,
|
||||
authSvc serviceauth.LoginStaffIntf,
|
||||
authSvc authsvc.AuthIntf,
|
||||
validate *validator.Validate,
|
||||
) {
|
||||
router.Post("/staff/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var request domain.StaffLoginReq
|
||||
var spec authdomain.LoginReq
|
||||
|
||||
if err := utils.UnmarshalBody(r, &request); err != nil {
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
@@ -33,7 +33,7 @@ func LoginStaff(
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(request); err != nil {
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
@@ -45,7 +45,7 @@ func LoginStaff(
|
||||
return
|
||||
}
|
||||
|
||||
token, err := authSvc.LoginAsStaff(request.Email)
|
||||
token, err := authSvc.LoginAsStaff(spec)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
@@ -58,7 +58,62 @@ func LoginStaff(
|
||||
return
|
||||
}
|
||||
|
||||
responsePayload := &domain.StaffLoginResponse{
|
||||
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,
|
||||
}
|
||||
|
||||
|
||||
@@ -5,5 +5,8 @@ import "go.uber.org/fx"
|
||||
var Module = fx.Module("auth-api",
|
||||
fx.Invoke(
|
||||
LoginStaff,
|
||||
LoginUser,
|
||||
RegisterUser,
|
||||
RegisterStaff,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
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 RegisterUser(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
authSvc authsvc.AuthIntf,
|
||||
) {
|
||||
router.Post("/user/register", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec authdomain.RegisterUserReq
|
||||
|
||||
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.RegisterUser(spec)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, token)
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterStaff(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
authSvc authsvc.AuthIntf,
|
||||
) {
|
||||
router.Post("/staff/register", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec authdomain.RegisterStaffReq
|
||||
|
||||
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.RegisterStaff(spec)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, token)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package authmiddleware
|
||||
|
||||
// import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
// "net/http"
|
||||
// "strings"
|
||||
|
||||
// redisaccessor "github.com/ardeman/project-legalgo-go/internal/accessor/redis"
|
||||
// contextkeyenum "github.com/ardeman/project-legalgo-go/internal/enums/context_key"
|
||||
// jwtclaimenum "github.com/ardeman/project-legalgo-go/internal/enums/jwt"
|
||||
// resourceenum "github.com/ardeman/project-legalgo-go/internal/enums/resource"
|
||||
// "github.com/ardeman/project-legalgo-go/internal/services/auth"
|
||||
// "github.com/golang-jwt/jwt/v5"
|
||||
// )
|
||||
|
||||
// const SessionHeader = "Authorization"
|
||||
|
||||
// func Authorization() func(next http.Handler) http.Handler {
|
||||
// return func(next http.Handler) http.Handler {
|
||||
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// ctx := r.Context()
|
||||
|
||||
// tokenString, err := GetToken(r)
|
||||
// if err != nil {
|
||||
// RespondWithError(w, r, err, "Invalid auth header")
|
||||
// return
|
||||
// }
|
||||
|
||||
// token, err := ValidateToken(ctx, tokenString)
|
||||
// if err != nil {
|
||||
// RespondWithError(w, r, err, err.Error())
|
||||
// return
|
||||
// }
|
||||
|
||||
// if isAuthorized, ctx := VerifyClaims(ctx, token, nil); !isAuthorized {
|
||||
// RespondWithError(w, r, errorcode.ErrCodeUnauthorized, errorcode.ErrCodeUnauthorized.Message)
|
||||
// return
|
||||
// } else {
|
||||
// next.ServeHTTP(w, r.WithContext(ctx))
|
||||
// return
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
// func GetToken(r *http.Request) (string, error) {
|
||||
// tokenString := GetTokenFromHeader(r)
|
||||
// if tokenString == "" {
|
||||
// tokenString = getTokenFromQuery(r)
|
||||
// }
|
||||
|
||||
// if tokenString == "" {
|
||||
// return "", fmt.Errorf("token not found")
|
||||
// }
|
||||
|
||||
// return tokenString, nil
|
||||
// }
|
||||
|
||||
// func GetTokenFromHeader(r *http.Request) string {
|
||||
// session := r.Header.Get(SessionHeader)
|
||||
// arr := strings.Split(session, " ")
|
||||
|
||||
// if len(arr) != 2 || strings.ToUpper(arr[0]) != "BEARER" {
|
||||
// return ""
|
||||
// }
|
||||
|
||||
// return arr[1]
|
||||
// }
|
||||
|
||||
// func getTokenFromQuery(r *http.Request) string {
|
||||
// token := r.URL.Query().Get("token")
|
||||
// return token
|
||||
// }
|
||||
|
||||
// func VerifyClaims(ctx context.Context, token *jwt.Token,
|
||||
// requiredResources []resourceenum.Resource) (bool, context.Context) {
|
||||
// if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
// rawResources := []interface{}{}
|
||||
// if claimValue, exist := claims[string(jwtclaimenum.RESOURCES)]; exist {
|
||||
// rawResources = claimValue.([]interface{})
|
||||
// }
|
||||
|
||||
// resources := []resourceenum.Resource{}
|
||||
// resourceMap := map[string]bool{}
|
||||
|
||||
// for _, v := range rawResources {
|
||||
// value := v.(string)
|
||||
// resources = append(resources, resourceenum.Resource(value))
|
||||
// resourceMap[value] = true
|
||||
// }
|
||||
|
||||
// ctx = context.WithValue(ctx, contextkeyenum.Authorization, UserAuthorization{
|
||||
// Type: claims[string(jwtclaimenum.TYPE)].(string),
|
||||
// UserId: claims[string(jwtclaimenum.AUDIENCE)].(string),
|
||||
// Username: claims[string(jwtclaimenum.USERNAME)].(string),
|
||||
// Resources: resources,
|
||||
// })
|
||||
|
||||
// isResourceFulfilled := false
|
||||
|
||||
// for _, v := range requiredResources {
|
||||
// if _, ok := resourceMap[string(v)]; ok {
|
||||
// isResourceFulfilled = true
|
||||
// ctx = context.WithValue(ctx, contextkeyenum.Resource, v)
|
||||
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
// if isResourceFulfilled || len(requiredResources) == 0 {
|
||||
// return true, ctx
|
||||
// }
|
||||
// }
|
||||
|
||||
// return false, nil
|
||||
// }
|
||||
|
||||
// func ValidateToken(ctx context.Context, tokenString string) (*jwt.Token, error) {
|
||||
// redisClient := redisaccessor.Get()
|
||||
// redisToken, err := redisClient.Exists(ctx, fmt.Sprintf("%s:%s", auth.BLACKLISTED_TOKEN_KEY, tokenString)).Result()
|
||||
|
||||
// if err != nil || redisToken > 0 {
|
||||
// return nil, fmt.Errorf("session already expired")
|
||||
// }
|
||||
|
||||
// token, err := jwt.Parse(tokenString, authsvc.VerifyToken(conf.JWTAccessToken))
|
||||
// if err != nil {
|
||||
// if ve, ok := err.(*jwt.ValidationError); ok {
|
||||
// if ve.Errors&jwt.ValidationErrorExpired != 0 {
|
||||
// err = errorcode.ErrCodeExpiredToken
|
||||
// }
|
||||
// }
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// return token, nil
|
||||
// }
|
||||
@@ -2,6 +2,7 @@ package internalhttp
|
||||
|
||||
import (
|
||||
authhttp "github.com/ardeman/project-legalgo-go/internal/api/http/auth"
|
||||
subscribeplanhttp "github.com/ardeman/project-legalgo-go/internal/api/http/subscribe_plan"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -16,6 +17,7 @@ var Module = fx.Module("router",
|
||||
validator.New,
|
||||
),
|
||||
authhttp.Module,
|
||||
subscribeplanhttp.Module,
|
||||
)
|
||||
|
||||
func initRouter() chi.Router {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package subscribeplanhttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
subscribeplandomain "github.com/ardeman/project-legalgo-go/internal/domain/subscribe_plan"
|
||||
subscribeplansvc "github.com/ardeman/project-legalgo-go/internal/services/subscribe_plan"
|
||||
"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 CreateSubscribePlan(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
subsSvc subscribeplansvc.SubsPlanIntf,
|
||||
) {
|
||||
router.Post("/subscribe-plan/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec subscribeplandomain.SubscribePlanReq
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if err := subsSvc.CreatePlan(spec.Code); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrCreateEntity.Code,
|
||||
response.ErrCreateEntity.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "success",
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package subscribeplanhttp
|
||||
|
||||
import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("subscribe-plan",
|
||||
fx.Invoke(
|
||||
CreateSubscribePlan,
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user