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