Deleting the current session with Rack::Session::Cookie
13,476
Solution 1
Just use
session.clear
to destroy the session.
Solution 2
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
Related videos on Youtube
Author by
ecoffey
A developer with an eye towards Compositional and Functional programming styles. Always striving to learn new ways to better express the Intent of programming over the Mechanics of implementation. Software Engineer at Twitter Boulder where I help to build large scale, high delivery systems and infrastructures.
Updated on August 29, 2020Comments
-
ecoffey over 3 years
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
-
ecoffey over 13 yearsHmm, I had tried that, but the rack.session cookie still exists after I "log out".
-
xentek about 10 years@ecoffey are you sure its the same cookie? If you use
session.clear
it'll likely be a new session, but the old one will havw been destroyed. Nil-ing session key(s) might keep the original cookie but be devoid of any value(s). -
Redoman over 8 yearsFrom the OP example it's clear (
enable :sessions
) that they are using Sinatra or a similar ruby framework, yet they asked about deleting the session usingRack::Session::Cookie
directly. Does the solution provided in this answer applies to that, or is Sinatra/Rails specific? According to stackoverflow.com/questions/10451392/…#session
is a method specific to Sinatra/Rails/... and plain rack applications don’t have it. If this is true then, does anybody know what's the correct rack-only way to deal with this? -
Ohad Perry over 8 yearsnot working for me, tried it a couple of times. I have a very simple app using
enable :sessions
-
Jonas Fagundes over 8 years@jj_ Nope,
sinatra
andrails
use rack middleware.