logo
down
shadow

plug in phoenix controller


plug in phoenix controller

By : Doyizode
Date : November 20 2020, 11:01 PM
wish helps you I would split the code in two steps - first attempting to authenticate the user, loading from the database and assigning to the connection - you can always run this one, and simply assign nil, a special guest user or nothing, whatever you prefer. The second would be enforcing the user is authenticated based on the assigns in the connection - this one would run only where you need to - like the plug you currently have.
code :


Share : facebook icon twitter icon
current_user in Phoenix controller passed by Plug

current_user in Phoenix controller passed by Plug


By : mobei
Date : March 29 2020, 07:55 AM
will be helpful for those in need Takes this code example as a Plug to handle authentication: , override action/2 and inject it:
code :
def action(conn, _) do
  apply(__MODULE__, action_name(conn),
    [conn, conn.params, conn.assigns.current_user])
end

def index(conn, _params, current_user) do
  ...
end

def show(conn, _params, current_user) do
  ...
end
stub Guardian.Plug.EnsureAuthenticated for Phoenix Controller testing

stub Guardian.Plug.EnsureAuthenticated for Phoenix Controller testing


By : Kevin Kulikowski
Date : March 29 2020, 07:55 AM
will be helpful for those in need I got my Phoenix Authorized Controllers tests pass. Here is my solution:
test/controllers/inbox_controller_test.exs:
code :
defmodule MyApp.InboxControllerTest do
  use MyApp.ConnCase
  alias MyApp.User

  setup do
    user = %User{id: "a1234"}
    {:ok, jwt, full_claims} = Guardian.encode_and_sign(user)
    {:ok, %{user: user, jwt: jwt, claims: full_claims}}
  end

  test "lists all entries on index", %{jwt: jwt} do
    conn = conn()
      |> put_req_header("authorization", "Bearer #{jwt}")
      |> get(inbox_path(conn, :index))
    assert json_response(conn, 200)["inboxes"] == []
  end
end
config :guardian, Guardian,
  serializer: MyApp.TestGuardianSerializer
defmodule MyApp.TestGuardianSerializer do
  @behaviour Guardian.Serializer

  alias MyApp.User

  def for_token(user = %User{}), do: { :ok, "User:#{user.id}" }
  def for_token(_), do: { :error, "Unknown resource type" }

  def from_token("User:" <> id) do
    {:ok, %User{id: id, login: "admin"}}
  end

  def from_token(_), do: { :error, "Unknown resource type" }
end
expected :action/2 to return a Plug.Conn error in Phoenix controller

expected :action/2 to return a Plug.Conn error in Phoenix controller


By : MA Rahman
Date : March 29 2020, 07:55 AM
I wish this help you You have an action in your controller that is not returning the conn. All actions must receive conn and return the updated conn.
Post processing the controller response with a plug in phoenix

Post processing the controller response with a plug in phoenix


By : Mega Mind
Date : March 29 2020, 07:55 AM
I hope this helps you . You can define a plug that uses register_before_send/2 and check the content-type header of the response (please note that Plug expects headers to be lowercase). A naive implementation (no error checking) would be:
How to read small chunks of data in phoenix controller, using Plug.Conn

How to read small chunks of data in phoenix controller, using Plug.Conn


By : Dagada
Date : March 29 2020, 07:55 AM
seems to work fine Plug.Conn.read_body/2 only reads one chunk of the request body. You need to call it recursively in order to read everything. You also don't need to write a Parser to just read the body in chunks (I don't think a Parser can even do that if I understand your question correctly); if the Content-Type of the request is not one that Plug parses by default, you can call Plug.Conn.read_body/2 from your controller.
Here's a small implementation of recursively calling Plug.Conn.read_body/2 from a controller:
shadow
Privacy Policy - Terms - Contact Us © soohba.com