redirect back to current page using omniauth and d

2020-03-31 08:35发布

问题:

I want to redirect back to current page after oauth signin, and I followed this Devise wiki and did the following:

  def after_sign_in_path_for(resource)
    sign_in_url = new_user_session_url
    if request.referer == sign_in_url
      super
    else
      request.env['omniauth.origin'] || stored_location_for(resource) || request.referer || root_path
    end
  end

request.env['omniauth.origin'] is http://www.bubutravel.com/users/sign_in

stored_location_for(resource) is nil

request.referer is http://openapi.qzone.qq.com/oauth/[omitted] (my provider)

So after my omniauth login, I get redirected to the provider urls again.

Is the wiki outdated? What's the recommended way to do omniauth login redirect to current page?

回答1:

I realized that I should not override after_sign_in_path_for as suggested by the wiki. I should just record the path in every request:

  before_action :store_current_location, :unless => :devise_controller?
  def store_current_location
    store_location_for(:user, request.url)
  end

And the build-in after_sign_in_path_for will handle the rest.