Dlacreme
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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mbuhot
This line looks suspect:
I’d usually expect something like:
Then in your view:
It’s expected that the conn is added to the assigns between the call to
renderin your controller and then Phoenix calling your viewsrenderfunction. Take a read of Phoenix.Controller.render to see how it works.Dlacreme
thanks for your reply @mbuhot
I’ve tried different formats and they all have the same results(the one you suggested included, I just tested) - like you suggested I ll take a look at the implementation of render, that might help to understand what’s going on. Thanks !
cenotaph
You have something in your assigns that plug conn cannot convert to html.
Ie you have some sort of embedded struct or module implementation or a method.
Your suth_nfo might be correct object but this line makes things worse.
Just do
token: tokenand test it out.cenotaph
Well there it is. io.inspect is not the same as render output method. How should render know how to convert
Userstruct into html?I am not sure where you got that piece of code,. It is not readable and open to massive exploits for security reasons.
Dlacreme
Thanks all for your reply.
@cenotaph the
|> render("200.json", %{auth_info})line is just one of many things I’ve tried to make it work. Once I got something that work I can clean the code. AlsoUsercorrectly implement Jason.Encode:Anyway, finally I changed some code for something even more basic and did that:
And it still fails with the same error:
I will just create a brand new app and try again. I must have made an error in the configs
amnu3387
I think the problem is you’re treating
renderas you would the helperjsonthat indeed takes a map or any json encodable element as the last and renders it as json.In this case I think you will have to do it as a regular assign, such as
render(conn, "vie.json", auth_info: auth_info)and then pick%{auth_info: auth_info}from the parameters on the json render.Dlacreme
I got it, I simply forgot
import Phoenix.Controllerat the top of my controller… sorry and thanks to all for your help.