简体   繁体   中英

Redirect from a specific page

I am trying to redirect users to the last page they logged in from (using a modal) - which is working.

if user  
    # Protects against session fixation attacks, causes request forgery
    # protection if user resubmits an earlier form using back
    # button. Uncomment if you understand the tradeoffs.
    # reset_session
    self.current_user = user
    new_cookie_flag = (params[:remember_me] == "1")
    handle_remember_cookie! new_cookie_flag
    format.html {
      if @invitation.try(:invitee_email) == user.email
        redirect_to(edit_invitation_path(@invitation))
      else
        begin
        # loop check
        if session[:last_back] != request.env['HTTP_REFERER']
        redirect_to(:back)
        session[:last_back] = request.env['HTTP_REFERER']
      else
        # raise on error
        raise ActionController::RedirectBackError
      end
        rescue ActionController::RedirectBackError
        # fallback on loop or other :back error
      redirect_to(:action => :index)
    end
 end

If the user fails to login - they are redirected to a login page at session/new.

I don't want them going back to this page after successful login so how would I redirect them to a new/path if they happen to be coming from session/new?

I think there are two ways to solve this problem:

  1. Instead of re-directing on login, use an Ajax request to post the data and simply don't redirect if the login fails. If the login succeeds, use javascript to redirect.
  2. Use a before_filter in your application controller that, when the user is logged-out, stores the current page, in the session, as the redirect page. Skip this filter on pages you don't want to remember, like the sessions/new page. After a user logs in, check the session for a redirect page and redirect to it if it exists. That way no matter how many times a user visits sessions/new they won't ever be redirected back there after a login. (You should probably do the same for the logout page as well - skip the before filter).

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