Session - values not set?

I might be doing something completely wrong here, but…

def create(conn, params) do
	put_session(conn, :gin, "tonic")
	Logger.debug "SESSION: #{inspect(get_session(conn))}"
	[...]
end

shouldn’t this controller action set the value for given key in the session?

What I get is only:

[debug] SESSION: %{"_csrf_token" => "cVSOEuWk1iGWN-iIfskUyNF6"}

And if I check for the exact key I used in put_session() I get nil. Any clues what I am doing wrong?

Configuration is the default one with endpoint.ex reading:

  # The session will be stored in the cookie and signed,
  # this means its contents can be read but not tampered with.
  # Set :encryption_salt if you would also like to encrypt it.
  @session_options [
    store: :cookie,
    key: "_<the_app_name>_key",
    signing_salt: "ELJOxI/O"
  ]

where <the_app_name> is of course the correct application name

Elixir data structures are immutable. You need to capture the result of put_session:

def create(conn, params) do
  conn = put_session(conn, :gin, "tonic")
  Logger.debug "SESSION: #{inspect(get_session(conn))}"
  # ...
end
1 Like

Gosh! Yes, absolutely! But that was actually only a debug error. My actual problem was something else - an “unsafe” variable (if I understand correctly the term): conn was assigned inside an unless block rather than being returned from it to an assignment. Yeah… Elixir is different than everything I used before :wink: @stefanchrobot thanks for pointing me in the right direction! Even if indirectly :slight_smile: