snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
25
internal/middleware/user_header_auth.go
Normal file
25
internal/middleware/user_header_auth.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/centrifugal/centrifuge"
|
||||
)
|
||||
|
||||
// UserHeaderAuth is a middleware that extracts the value of user ID from the specific header
|
||||
// and sets connection credentials.
|
||||
func UserHeaderAuth(userHeaderName string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
userID := r.Header.Get(userHeaderName)
|
||||
if userID != "" {
|
||||
ctx := centrifuge.SetCredentials(r.Context(), ¢rifuge.Credentials{
|
||||
UserID: userID,
|
||||
})
|
||||
r = r.WithContext(ctx)
|
||||
}
|
||||
// Call the next handler
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user