Skip to content

Commit 5018f51

Browse files
updated middleware for username context passing to handlers
1 parent f30344c commit 5018f51

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

api/middleware/middleware.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package middleware
22

33
import (
4+
"context"
45
"net/http"
56
"time"
67

7-
"github.com/PythonHacker24/linux-acl-management-backend/internal/auth"
8+
"github.com/PythonHacker24/linux-acl-management-backend/internal/token"
89
"go.uber.org/zap"
910
)
1011

@@ -34,9 +35,8 @@ func LoggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
3435
/* authentication middleware for http requests */
3536
func AuthenticationMiddleware(next http.HandlerFunc) http.HandlerFunc {
3637
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37-
3838
/* authenticate the request through JWT */
39-
username, err := auth.ExtractUsernameFromRequest(r)
39+
username, err := token.ExtractUsernameFromRequest(r)
4040
if err != nil {
4141
zap.L().Error("Error during authentication",
4242
zap.Error(err),
@@ -47,7 +47,10 @@ func AuthenticationMiddleware(next http.HandlerFunc) http.HandlerFunc {
4747
/* set the header with the username */
4848
r.Header.Set("X-User", username)
4949

50+
/* pass username as context */
51+
ctx := context.WithValue(r.Context(), "username", username)
52+
5053
/* return the handler */
51-
next(w, r)
54+
next(w, r.WithContext(ctx))
5255
})
5356
}

0 commit comments

Comments
 (0)