fix: get profile user/staff using token rather than id
This commit is contained in:
@@ -4,7 +4,9 @@ import (
|
||||
"errors"
|
||||
authsvc "legalgo-BE-go/internal/services/auth"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
@@ -13,14 +15,15 @@ func GetStaffProfile(
|
||||
router chi.Router,
|
||||
authSvc authsvc.AuthIntf,
|
||||
) {
|
||||
router.Get("/staff/{id}/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||
router.Get("/staff/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
id := chi.URLParam(r, "id")
|
||||
if id == "" {
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
|
||||
if authHeader == "" {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
errors.New("provided id is empty"),
|
||||
errors.New("provided auth is empty"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"required params is not provided",
|
||||
@@ -28,7 +31,45 @@ func GetStaffProfile(
|
||||
return
|
||||
}
|
||||
|
||||
staffProfile, err := authSvc.GetStaffProfile(id)
|
||||
if !strings.HasPrefix(authHeader, "Bearer") {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
errors.New("invalid authorization token"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"invalid required token",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token := strings.Split(authHeader, " ")
|
||||
if len(token) < 2 {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
errors.New("invalid authorization"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"invalid required token",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
destructedToken, err := utils.DestructToken(token[1])
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
staffProfile, err := authSvc.GetStaffProfile(destructedToken.Email)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
@@ -49,14 +90,15 @@ func GetUserProfile(
|
||||
router chi.Router,
|
||||
authSvc authsvc.AuthIntf,
|
||||
) {
|
||||
router.Get("/user/{id}/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||
router.Get("/user/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
id := chi.URLParam(r, "id")
|
||||
if id == "" {
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
|
||||
if authHeader == "" {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
errors.New("provided id is empty"),
|
||||
errors.New("provided auth is empty"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"required params is not provided",
|
||||
@@ -64,7 +106,45 @@ func GetUserProfile(
|
||||
return
|
||||
}
|
||||
|
||||
userProfile, err := authSvc.GetUserProfile(id)
|
||||
if !strings.HasPrefix(authHeader, "Bearer") {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
errors.New("invalid authorization token"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"invalid required token",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token := strings.Split(authHeader, " ")
|
||||
if len(token) < 2 {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
errors.New("invalid authorization"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"invalid required token",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
destructedToken, err := utils.DestructToken(token[1])
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user