简体   繁体   中英

Devise redirect after login or logout doesn't work with Rails page caching

I'm using Devise for authentication in my Rails app on Heroku.

I have redirects after login and logout setup the way it's suggested to do so in the Devise wiki. ( https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update )

application_controller.rb:

after_filter :store_location

def store_location
  # store last url as long as it isn't a /users path
  session[:previous_url] = request.fullpath unless (request.fullpath =~ /\/users\/sign_in/ || request.fullpath =~ /\/users\/sign_out/ || request.fullpath =~ /\/users\/sign_up/ || request.fullpath =~ /\/users\/edit/ || request.fullpath =~ /\/ajax_utilities/ || request.fullpath =~ /\/assets/)
end

def after_sign_in_path_for(resource)
  session[:previous_url] || root_path
end

def after_sign_out_path_for(resource)
  session[:previous_url] || root_path
end

The redirects work fine until I enable page caching with code like this:

class AboutController < ApplicationController

  def index
      expires_in 1.minutes, public: true
  end

end

I'm 99% sure that store_location isn't ever being called because the way page caching works in Rails the application controller would never even be reached because Rack serves the static HTTP without touching Rails.

Does anyone have any ideas about how I can take advantage of Rails page caching while also having my redirects after login/logout work correctly for Devise?

Thanks for any help.

Try a before filter in the AboutController

before_filter :destroy_cache

def destroy_cache
  response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
  response.headers["Pragma"] = "no-cache"
  response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end

Not sure its exactly what you need but a starting point perhaps

Take a look at this post and the original citation

Update:

Take a look at this article . It will show ou how to set up friendly forwarding which sounds like what you want to do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM