Reputation: 527
I have the following code:
enable :sessions
class CSFSWC < Sinatra::Application
post '/login' do
username = params[:username]
password = params[:password]
if csfsss.authenticate(username,password) == false
redirect '/'
# session[:loginsession] = nil
else
# Start session here
session[:username] = params[:username]
@sessionID = session[:username]
puts "Session : #{@sessionID}"
redirect '/main'
end
get '/main' do
puts "main session : #{@sessionID}"
end
In /main the puts print out a empty session[:username]. Any ideas? As far as I understand sessions in Sinatra according to: Sinatra Sessions.
It should just work.
In fact even if I copy the example from Sinatra Sessions
class CSFSWC < Sinatra::Base
enable :sessions
get '/foo' do
session[:message] = 'Hello World!'
puts "foo : #{session[:message]}"
redirect '/bar'
end
get '/bar' do
puts "bar : #{session[:message]}"
end
It still does not work?
Upvotes: 1
Views: 846
Reputation: 5301
It's working just fine - look at your app's output to the console. It's just not doing what you expect. puts
writes to stdout, not to the browser. With Sinatra, the last string you return is what is sent to the browser. This is what you want:
class CSFSWC < Sinatra::Base
enable :sessions
get '/foo' do
session[:message] = 'Hello World!'
# This just writes it to your log
puts "foo : #{session[:message]}"
redirect '/bar'
end
get '/bar' do
"bar : #{session[:message]}"
end
end
Upvotes: 1