Dlacreme

Dlacreme

Phoenix - Unexpected behavior on `render`: render receives Plug.Conn as second parameters

Hi,

I have some experience with Phoenix Liveview but I never had this kind of issue so I hope you can help. I just created a brand new application and I am implementing a Oauth2 auth using ueberauth_facebook

The oauth2 flow is working well, but when I try to return the newly authenticated user and the related JWT, the render function crashes. The log shows that render received a full Plug.Conn%{} struct a second parameter, which doesn’t make any sense.

My routes are setup for JSON (pipe_through :api)

  scope "/auth", PlxWeb do
    pipe_through :api

    get "/:provider/callback", AuthController, :callback
    post "/:provider/callback", AuthController, :callback
    # ...
  end

My controller/handler is quite simple:

  def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
    IO.puts("Callback success")

    case Auth.authenticate_from_callback(auth) do
      {:ok, %{token: _t, user: _u} = auth_info} ->
        IO.puts("user authenticated > #{inspect(auth_info)}")
        conn
        |> put_status(:ok)
        |> render("200.json", %{auth_info})

      {:error, reason} ->
        IO.puts("failed to authenticated user")
        conn
        |> put_status(:internal_server_error)
        |> render("500.json", reason)
    end
  end

And my view and smaller:

  def render("200.json", %{token: _t, user: _u} = payload) do
    IO.puts("Payload > #{inspect(payload)}")
    %{message: "ok", payload: payload}
  end

This code prints the following logs:

> "Callback success"
# means the provider authentication was ok
> "user authenticated > %{token: 'expected token', user: %User{id: xxx, name: 'im a normal user'}}"
# so far all good
> "Payload >  %{conn: %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{layout: false, ueberauth_auth: %Ueberauth.Auth{credentials: (+20 lines)"
# why did my token/user map transformed to this?
-> crash: "`protocol Jason.Encoder not implemented for %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{kind: :error, layout: false` (+20 lines)"
# which makes sense since the Plug.Conn struct is not expected to be encoded

I don’t have a stacktrace of what’s happening. I’ve been stuck for a couple of hours, I’ve tried difference thing. I don’t even understand how the pattern matching in the view doesn’t fail

Thanks for your kind help, it’s probably some stupid mistakes

Marked As Solved

Dlacreme

Dlacreme

I got it, I simply forgot import Phoenix.Controller at the top of my controller… sorry and thanks to all for your help.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement