Raghvendra Parashar
Raghvendra Parashar

Reputation: 4053

Do we need to delete session variables after user logout?

In Ruby on Rails :

suppose I am having session[:my_var] = 'my_val'

So here my question is : Do we need to set session[:my_var]=nil before user's sign-out?

or it will auto release the memory it has.

Upvotes: 5

Views: 8294

Answers (3)

jbearden
jbearden

Reputation: 1869

If you have set up your authentication code properly, that should handle the release from memory. Here is how I did it in one of my apps:

#sessions_controller
  def destroy
    reset_session
    redirect_to login_path, notice: 'Logged out'
  end

Upvotes: 2

achedeuzot
achedeuzot

Reputation: 4404

Ruby on Rails doesn't know what you want to keep or not when a user signs-out.

Say for example you have a session[:language] that is useful for every user, even anonymous ones. You wouldn't want to erase it to display the default language after the user has gone through the trouble of selecting one in particular.

So, delete the session objects you need to, like session[:user]=nil and keep the rest. If you have a lot of them to delete, make yourself a logout helper.

If you know you can swipe the whole session, use the reset_session like @adcosta said.

Upvotes: 5

adc
adc

Reputation: 781

If you want to clean your app session use reset_session.

If you only want to clean a var do session[:my_var]=nil

See Sessions in rails

Upvotes: 3

Related Questions