Перенес основы lab3 в lab4
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model"
|
||||
)
|
||||
|
||||
type userContextKey struct{}
|
||||
|
||||
func (api *API) authenticate(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := api.messenger.CurrentUser(r.Header.Get("X-User-Id"))
|
||||
if err != nil {
|
||||
writeAppError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), userContextKey{}, user)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func userFromContext(ctx context.Context) model.User {
|
||||
return ctx.Value(userContextKey{}).(model.User)
|
||||
}
|
||||
Reference in New Issue
Block a user