Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between assign and put_session in Plug.Conn of the Phoenix Framework?

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?

like image 693
Maarten Faddegon Avatar asked Sep 30 '17 11:09

Maarten Faddegon


1 Answers

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.

like image 185
PatNowak Avatar answered Oct 25 '22 16:10

PatNowak