Question

I created a simple authentication for Sinatra, however the session object seems to be cleaning up ALL custom keys. For example, when user logs in:

session[:user_id] = current_user.id

This is effectively stored in the session object for the current request. When a new request occurs the session[:user_id] is not there anymore. Session is active, cookies are enabled. I tried everything I can't figure out what it wrong (here is the all the relevant code: https://gist.github.com/ksiomelo/7656296).

application:

 use Rack::Session::Cookie , :secret => "82e042cd6fde2bf1764f777236db799e"

 enable :sessions # for flash messages

helpers:

 def require_auth
  unless session[:user_id]
    flash[:error] = "You need to be logged in."
    redirect to("/login")
  end
end

def current_user
  @current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
end

signin:

   authorized_user = User.authenticate(params[:email],params[:password])
      if authorized_user
        # update session / redirect
        session[:user_id] = authorized_user.mongo_id.to_s 
        session.options[:expire_after] = 2592000 unless params[:remember].nil? # 30 days

        # redirect to the wizard
        flash[:info] = "Welcome back #{authorized_user.first_name}"
        redirect to("/home")
Was it helpful?

Solution

You should likely try to set a provider for session handling, e. g.:

use Rack::Session::Pool, :expire_after => 2592000

Glad to help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top