whoisthis/middleware.go
2025-04-07 05:36:03 -06:00

25 lines
521 B
Go

package whoisthis
import (
"context"
"net/http"
)
type CtxKey string
const ContextUserId CtxKey = "userid"
func (wis WhoIsThis) Authed(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Our middleware logic goes here...
user, err := wis.LookupReqFn(r)
if err != nil {
wis.DenyFn(w, r)
return
}
ctxWithUser := context.WithValue(r.Context(), ContextUserId, user)
rWithUser := r.WithContext(ctxWithUser)
next.ServeHTTP(w, rWithUser)
})
}