vendredi 6 mars 2015

How reload or refresh page when session has expired with Ruby on Rails

Good morning!


We are implementing session control for our app, we need to reload page automatically to go to login page when session expires or show a modal or pop-up to make user know that the session has expired and take user to login page, we have tried a lot of options to make it, but anything works for us.


This is our context:


application_controller.rb



before_filter :prepare_session

def prepare_session

if !session[:expiry_time].nil? and session[:expiry_time] < Time.now
# Session has expired. Clear the current session.
deny_access 'Your session has timed out. Please log back in.'
end

# Assign a new expiry time, whether the session has expired or not.
session[:expiry_time] = 1.minutes.from_now
return true
end


session_helper.rb



def deny_access(msg = nil)
msg ||= 'Please sign in to access this page.'
flash[:notice] ||= msg
redirect_to :controller => 'users', :action => 'login'
end


and inside deny_access method after flash message, we have redirect_to and our login function controller, but it just do that in background, and ruby console give us this log


RubyMine log



Redirected to http://localhost:3000/users/login
Filter chain halted as :prepare_session rendered or redirected
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)


Started GET "/users/login" for 127.0.0.1 at 2015-03-06 11:28:06 -0500
Processing by UsersController#login as JSON
Rendered users/login.html.erb within layouts/application (0.8ms)
Completed 200 OK in 1089ms (Views: 1087.4ms | ActiveRecord: 0.0ms)


Despite the message telling us that view users/login.html.erb have been redirected, nothing happens in our app, just until we reload page manually, so, these are all options that we have tried:


redirect_to:



redirect_to :controller => 'users', :action => 'login'
redirect_to url_for(:controller => :users, :action => :login)
redirect_to 'users/login'
redirect_to :back
redirect_to root_url
redirect_to(request.env["HTTP_REFERER"])


respond_to:


for this test, we created a redirect_to_login.js.erb file inside /views/users/ that file contains a window.location.reload(), but It never arrives to that file, therefore, our page is never reloaded



respond_to do |format|
format.html{
redirect_to 'users/login'
}
format.js {
render 'users/redirect_to_login', :layout=>false
}
end
respond_to do |format|
format.html
format.js {
render nothing: true
}
end
respond_to do |format|
format.js {render inline: "location.reload();" }
end


Any suggestions what's going on? or what should we do? We are going to be grateful for help


greetings!


Aucun commentaire:

Enregistrer un commentaire