package api import ( "context" "github.com/google/uuid" "completed/internal/model" ) // authedUser is what AuthMiddleware stores in the request context after // validating a session token. Handlers read it with userFromContext. type authedUser struct { ID uuid.UUID Email string Name string Bio string IsAdmin bool } type ctxKey string const userCtxKey ctxKey = "completed.user" func (u authedUser) toModel() model.User { return model.User{ID: u.ID.String(), Email: u.Email, Name: u.Name, Bio: u.Bio, IsAdmin: u.IsAdmin} } // userFromContext returns the authenticated user set by AuthMiddleware. It // panics if called from a route that isn't behind AuthMiddleware, since that // is a programming error (a handler trusting a context value that was never // guaranteed to be there), not a condition callers should need to check. func userFromContext(ctx context.Context) authedUser { u, ok := ctx.Value(userCtxKey).(authedUser) if !ok { panic("api: userFromContext called outside AuthMiddleware") } return u }