It’s not clear to me exactly how you show the “middleware” or how it is executed. Since you only change a local variable rand never give it to anyone else, you cannot change it outside of your function.
, , 1.7, :
func IsAuthenticated(next http.Handler) http.Handler{
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
//authenticate. Set cookies.
//403 if not authenticated, etc.
ctx := context.WithValue(r.Context(), "user",u)
next(w,r.WithContext(ctx))
})
}
:
http.Handle("/foo/bar", IsAuthenticated(myHandler))
( - alice, .)
, myHandler, , IsAuthenticated, - :
user := r.Context().Get("user").(*User)