Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove previous sessions in Rails, if user forget to logout and close the browser?
    text
    copied!<p>I store all session information in database. The user can login the system for do something. There are some restricted area required user login. If the user try to reach those area before login, the system will redirect them to login page.</p> <p>Sometimes user may close the browser without logout. If this happen, next time when he try to login, the system cannot recognized the newly generate session, even the user typed in the correct username and password. The system will then force the user login again.</p> <p>Would anyone know how to fix this? I tried to reset the session using <code>reset_session</code> before loading the login page. The problem cannot be fixed.</p> <p><strong>Session Controller</strong></p> <pre><code> def new end def create session[:current_account] = Account.authenticate(params[:email], params[:password]) if session[:current_account] redirect_to :controller =&gt; "xxxxx", :action =&gt; "index" else flash[:notice] = "Please try again." render :action =&gt; 'new' end end </code></pre> <p><strong>Account Model</strong></p> <pre><code> def self.authenticate(email, pass) account = find_by_email(email) account &amp;&amp; account.authenticated?(pass) ? account : nil end def authenticated?(pass) encrypted_password == Account.encrypt(pass,salt) end </code></pre> <p><strong>Xxxxx Controller</strong></p> <pre><code> before_filter :permission_handling def index end </code></pre> <p><strong>Application Controller</strong></p> <pre><code> def permission_handling unless logged_in? forced_login end end def logged_in? session[:current_account].is_a?(Account) end def forced_login flash[:notice] = "You haven't login yet!" redirect_to ( :controller =&gt; "sessions", :action =&gt; "new" ) end </code></pre> <p>Thanks all. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload