The documentation (https://hexdocs.pm/plug/Plug.Conn.html) names two functions that allow for storing a key-value pair in a conn
assign(conn, key, value)
Assigns a value to a key in the connection
put_session(conn, key, value)
Puts the specified value in the session for the given key
What is the difference between these two functions?
Basically assign
works only for particular connection - Plug.Conn structure - which means that this value would be gone with the end of the request - as conn will also dies - at the end of the request / response cycle.
Session lives longer and it's stored in cookies or in ets. Thanks to that you keep eg. data about the successfully logged in user across the requests.
TL;DR:
assign
works for each request and it's wipe out after the end of the request cycle.
put_session
inserts a value in the session and it's available until the session is cleared / expired.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With