开发者

logout should redirect to :back unless :back requires a user

开发者 https://www.devze.com 2023-01-12 11:43 出处:网络
When logout is clicked I want to redirect back to the page the user was on unless that page requires a user to be logged in.

When logout is clicked I want to redirect back to the page the user was on unless that page requires a user to be logged in.

scenario 1 - "back" requires a user 1) User clicks logout from user account page 2) redirected (ultimately) to root_path

scenario 2 - "back" does not require a user 1) User clicks logout from (not user required) events page 2) redirected (ultimately) to events_path

It's been suggested I update my before_filter :require_use method to be something like this:

  def require_user
    unless current_user
      flash[:notice] = "You must be logged in to view this page"
      redirect_to user_coming_from_logout_method? root_path : login_path
    end 
  end 

Except I've been unable to define #user_coming_from_logout_method?

One might think that it is simply:

request.referrer == "/logout"

But in the above example request.referrer 开发者_Python百科actually == "/user_account", not the intermediate logout method. Very weird, I know.

Suggestions?


I don't think you can rely on the browser to set the referrer on a redirect. This thread discusses that issue in some detail.

Maybe set something in session in the logout controller, which you test for and then clear in the require_user method?

0

精彩评论

暂无评论...
验证码 换一张
取 消