28 lines
630 B
Go
28 lines
630 B
Go
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)
|
|
}
|