Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir/Phoenix - Accessing user's cookie: conn.cookies vs conn.req_cookies vs conn.req_headers

So i'm trying to implement something like Imgur, where an user who isn't a member on the website can upload, and continue to edit that upload (until the cookie is reset).

When looking at the conn struct, there are 3 places where the user's cookie appears.

conn.cookies["_APPNAME_key"]
conn.req_cookies["_APPNAME_key"]
List.keyfind(conn.req_headers, "cookie", 0)

These all return the same cookie (except the last one has "_APPNAME_key=COOKIE" vs "COOKIE" for the other 3)

What is the difference between the three? Is there one I should use instead of the others?

like image 759
Peter R Avatar asked Jun 28 '18 06:06

Peter R


1 Answers

  • conn.cookies the request cookies with the response cookies
  • conn.req_cookies the request cookies (without the response ones), of course there is coon.resp_cookies for the response cookies.

List.keyfind(conn.req_headers, "cookie", 0) just get the cookies from request's headers, which is conn.req_cookies.

You should use conn.req_cookies["_APPNAME_key"] in your case.

See https://hexdocs.pm/plug/Plug.Conn.html#module-request-fields for more details.

like image 185
chris Avatar answered Oct 15 '22 03:10

chris