participateapp / api

API backing Participate!, an app for democratic proposal-making
MIT License
1 stars 0 forks source link

Make current user globally accessible through conn #31

Open oliverbarnes opened 7 years ago

oliverbarnes commented 7 years ago

Seems like the only way to access controller context in ja_serializer views is through conn. So for payload attributes that depend on the logged in participant info, we're doing stuff like

# in ProposalController
def index(conn, _params, account, _claims) do
  conn = assign(conn, :account, account)
...

def show(conn, %{"id" => id}, account, _claims) do
  conn = assign(conn, :account, account)
...

# then, in ProposalView
def authored_by_me(proposal, conn) do
  if proposal.author.id == conn.assigns[:account].participant_id do
    "true"
  else
    "false"
  end
end

We could at least make this automatically available on controller actions once the user is logged in, like Rails / Devise's current_user method.