Taras Kovalenko
Taras Kovalenko

Reputation: 2393

Ruby and Sinatra problems with session

I used sinatra session in my ruby project. After login I write in session[:name] the name of user. If user call some API method I verify session[:name] and if this value is not empty he get response from the server. After logout I clear this value. My code:

 use Rack::Session::Cookie, :expire_after => 86400

    get '/login' do
        session[:name] = params[:username]
    end

    get '/logout' do
        session[:name] = ''
        return 'done'
    end

    error 401 do
      return '401 Unauthorized'
    end

    get '/check_session' do
        if session[:name].to_s.strip.length == 0 || session[:name].to_s!=params[:username]
            return 401
        end
        return session[:name]
    end

This code works good. But if I login from one browser, i.e. Google Chrome, and after that open anther browser, i.e. Mozilla FireFox and call /check_session I get response from server 401 Unauthorized. Why does this happen? And how to fix it?

Upvotes: 2

Views: 142

Answers (2)

rohitpaulk
rohitpaulk

Reputation: 427

That isn't a bug, it's expected. A session only lives on one browser.When you launch Firefox, you start a new session. :)

Upvotes: 1

OlegL
OlegL

Reputation: 359

its not a bug. Each browser making own session. And you need to do login after open page in new browser.

Upvotes: 2

Related Questions