View Source Charon.TokenPlugs.PutAssigns (Charon v1.3.4)

After verifying everything you would want to verify about a token, assign the following to the conn:

All of the assign names are overridable:

# assign the user ID to key :current_user_id
plug PutAssigns, user_id: :current_user_id

doctests

Doctests

iex> opts = PutAssigns.init([])
iex> conn = conn() |> put_private(@bearer_token_payload, %{"sub" => 1, "sid" => "a"})
iex> conn |> PutAssigns.call(opts) |> Map.get(:assigns)
%{session_id: "a", token_payload: %{"sid" => "a", "sub" => 1}, user_id: 1}

iex> opts = PutAssigns.init(session: :da_session_baby)
iex> conn = conn() |> put_private(@bearer_token_payload, %{"sub" => 1, "sid" => "a"}) |> put_private(@session, "hii")
iex> conn |> PutAssigns.call(opts) |> Map.get(:assigns)
%{
  session_id: "a",
  token_payload: %{"sid" => "a", "sub" => 1},
  user_id: 1,
  da_session_baby: "hii"
}

# skipped on auth error
iex> opts = PutAssigns.init([])
iex> conn = conn() |> put_private(@bearer_token_payload, %{"sub" => 1, "sid" => "a"}) |> Internal.auth_error("boom")
iex> conn |> PutAssigns.call(opts) |> Map.get(:assigns)
%{}