Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting the current session with Rack::Session::Cookie

I feel like I'm missing something obvious here, and I'm hoping that as soon as I post this someone will shame me with the google search link I was missing :-)

enable :sessions  get '/logout' do   # What goes here to kill the session? end 
like image 787
ecoffey Avatar asked Nov 23 '10 04:11

ecoffey


People also ask

When session cookie is removed?

Persistent cookies are cookies that are saved on your computer and are not deleted automatically when you quit your browser, unlike a session cookie, which is deleted when you leave your browser.

What is rack session?

Rack::Session::Cookie provides simple cookie based session management. By default, the session is a Ruby Hash stored as base64 encoded marshalled data set to :key (default: rack. session). The object that encodes the session data is configurable and must respond to encode and decode .

How do I delete a session in rails?

To clear the whole thing use the reset_session method in a controller. Resets the session by clearing out all the objects stored within and initializing a new session object.


2 Answers

Just use

session.clear 

to destroy the session.

like image 144
Jonas Fagundes Avatar answered Oct 23 '22 16:10

Jonas Fagundes


It depends how you create your session. Simply you have to nulify session entry. Here is simple example, how to create and destroy sessions.

  get '/login' do     session[:username] = params[:username]     "logged in as #{session[:username]}"   end    get '/logout' do     old_user = session[:username]     session[:username] = nil     "logged out #{old_user}"   end 

You can also check this example: https://gist.github.com/131401

like image 38
tjeden Avatar answered Oct 23 '22 17:10

tjeden