// Package reqcontext provides a shared context key and helpers for storing
// the authenticated user in a request context. It lives in its own package so
// that both httpapi (middleware) and httpapi/manage (handlers) can import it
// without creating an import cycle.
package reqcontext

import (
	"context"

	"git.az-it.net/az/morz-infoboard/server/backend/internal/store"
)

type contextKey int

const contextKeyUser contextKey = 0

// WithUser returns a new context that carries u.
func WithUser(ctx context.Context, u *store.User) context.Context {
	return context.WithValue(ctx, contextKeyUser, u)
}

// UserFromContext returns the *store.User stored in ctx, or nil if none.
func UserFromContext(ctx context.Context) *store.User {
	u, _ := ctx.Value(contextKeyUser).(*store.User)
	return u
}
