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

2020-05-24 20:51发布

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

2条回答
劳资没心,怎么记你
2楼-- · 2020-05-24 21:13

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

查看更多
时光不老,我们不散
3楼-- · 2020-05-24 21:30

Just use

session.clear

to destroy the session.

查看更多
登录 后发表回答