Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

put_resp_header error after api_sign_in #196

Closed
kevin-DL opened this issue Sep 4, 2016 · 3 comments
Closed

put_resp_header error after api_sign_in #196

kevin-DL opened this issue Sep 4, 2016 · 3 comments

Comments

@kevin-DL
Copy link

kevin-DL commented Sep 4, 2016

Hello,
I am going through the simple api authentication blog post.

When i try to insert the header in to the response i get the following error
** (exit) an exception was raised: ** (FunctionClauseError) no function clause matching in Plug.Conn.put_resp_header/3

My code looks like this

` defp update_user(conn, user, user_params) do

changeset = User.changeset(user, User.format_info(user_params))
case Repo.update(changeset) do
  {:ok, user_updated} ->
      new_conn = Guardian.Plug.api_sign_in(conn, user_updated)
      jwt = Guardian.Plug.current_token(new_conn)
      {:ok, claims} = Guardian.Plug.claims(new_conn)
      exp = Map.get(claims, "exp")

      new_conn = Plug.Conn.put_resp_header(new_conn,"authorization", "Bearer #{jwt}")
      new_conn = Plug.Conn.put_resp_header(new_conn,"x-expires", exp)
      new_conn
      |> render "loggedIn.json", user: user, jwt: jwt, exp: exp
    {:error, reason} ->
      conn
      |> put_status(401)
      |> render "error.json", errors: "Could not login"
end

end `

Do you have any idea?
Regards,
K

@keithalpichi
Copy link

keithalpichi commented Sep 4, 2016

You're passing exp as an integer and not a string.
Fix it from this:
Plug.Conn.put_resp_header(new_conn,"x-expires", exp)
to this:
Plug.Conn.put_resp_header(new_conn, "x-expires", "#{exp}")

I'd also recommend to take advantage of the pipe operator like so:

new_conn
  |> put_status(200)
  |> put_resp_header("authorization", "Bearer #{jwt}")
  |> put_resp_header("x-expires", "#{exp}")
  |> render("loggedIn.json", %{user: user, jwt: jwt, exp: exp})

I'm sure this this function is in a controller and is __using__ the :controller behavior. So you don't have to prefix put_resp_header with Plug.Conn as it's already imported. So just using put_resp_header will do like the example I gave above.

@kevin-DL
Copy link
Author

kevin-DL commented Sep 4, 2016

@keithalpichi Thank you that fixed it.
It might be necessary to update the blog post though i am probably the only one who would not notice.
Closing the issue

@kevin-DL kevin-DL closed this as completed Sep 4, 2016
@keithalpichi
Copy link

@kevin-DL you're welcome. I agree with you. Hopefully the authors of the article see this issue and fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants