whoisthis/middleware.go

26 lines
561 B
Go
Raw Normal View History

2025-01-12 11:47:44 +00:00
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 {
http.Error(w, "please sign-in", http.StatusUnauthorized)
return
}
ctxWithUser := context.WithValue(r.Context(), ContextUserId, user)
rWithUser := r.WithContext(ctxWithUser)
next.ServeHTTP(w, rWithUser)
})
}