Uses this sample code as an authentication plug-in:
defmodule Financeweb.APIAuth do ... def call(conn, _opts) do ... if authenticated_user do conn |> assign(:current_user, user) else conn |> send_resp(401, "{\"error\":\"unauthorized\"}") |> halt end end end
So, I pass the current_user variable downstream through Plug.Conn.assign/3 . What is the best way to get this variable in a Phoenix controller? I do this (code below), but I'm sure there is a better way to do this.
def index(conn, _) do user_id = conn.assigns.current_user.id end
elixir phoenix-framework
Nando sousa
source share