-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d03d908
commit 3654db5
Showing
3 changed files
with
12 additions
and
9 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invalid login : sql: no rows in result set |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,43 @@ | ||
package logger_middleware | ||
|
||
import ( | ||
commonerrors "lets-go/libs/commonErrors" | ||
localconstants "lets-go/libs/localConstants" | ||
"net/http" | ||
"slices" | ||
) | ||
|
||
type AdminGuar struct { | ||
type AdminGuard struct { | ||
handler http.Handler | ||
} | ||
|
||
func (a *AdminGuar) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
protectedData, ok := r.Context().Value("protected_data").(map[string]interface{}) | ||
func (a *AdminGuard) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
protectedData, ok := r.Context().Value(localconstants.PROTECTED_DATA_KEY).(map[string]interface{}) | ||
if !ok { | ||
http.Error(w, "server error: invalid protected data type", http.StatusInternalServerError) | ||
commonerrors.HttpErrorWithMessage(w, nil, http.StatusInternalServerError, "invalid protected data type") | ||
return | ||
} | ||
|
||
userRoles, exists := protectedData["userRoles"] | ||
if !exists { | ||
http.Error(w, "server error: userRoles key missing", http.StatusInternalServerError) | ||
commonerrors.HttpErrorWithMessage(w, nil, http.StatusInternalServerError, "userRoles key missing") | ||
return | ||
} | ||
|
||
roles, ok := userRoles.([]string) | ||
if !ok { | ||
http.Error(w, "server error: userRoles has invalid type", http.StatusInternalServerError) | ||
commonerrors.HttpErrorWithMessage(w, nil, http.StatusInternalServerError, "userRoles has invalid type") | ||
return | ||
} | ||
|
||
if !slices.Contains(roles, "Admin") { | ||
http.Error(w, "server error: You are not allowed use this feature", http.StatusForbidden) | ||
commonerrors.HttpErrorWithMessage(w, nil, http.StatusForbidden, "You are not allowed use this feature") | ||
return | ||
} | ||
a.handler.ServeHTTP(w, r) | ||
} | ||
|
||
// NewLogger constructs a new Logger middleware handler | ||
func NewLogger(handlerToWrap http.Handler) *AdminGuar { | ||
return &AdminGuar{handlerToWrap} | ||
func NewAdminGuard(handlerToWrap http.Handler) *AdminGuard { | ||
return &AdminGuard{handlerToWrap} | ||
} |