我认为你最好的选择是做几件事:
没有设计
我会为您的策略添加一个将在设置阶段运行的块。您需要将此添加到要配置的每个策略中。
https://github.com/omniauth/omniauth/wiki/Setup-Phase
SETUP_PROC = lambda do |env|
request = Rack::Request.new(env)
session = request.env['rack.session']
session[:preauth_referrer] ||= request.referrer
end
use OmniAuth::Builder.new do
provider :google, :setup => SETUP_PROC
end
更改配置代码后,您可能需要重新启动应用程序服务器。
使用设计
-
如果您还没有覆盖 OmniauthCallbacksController,请覆盖
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
end
# Then in routes.rb, as detailed at https://github.com/plataformatec/devise/wiki/omniauth:-overview
devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' }
-
在刚刚创建的新控制器中添加一个 before_filter。 /auth/:provider 端点默认使用 passthrough 方法。
prepend_before_action :store_referrer, only: [:passthrough]
-
将引荐来源网址存储在会话中。这是必要的,因为您尚未对用户进行身份验证
session[:preauth_referrer] = request.referrer
所以完整的控制器如下所示:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
prepend_before_filter :store_referrer, only: [:passthrough]
# Other overrides go here, for example overriding the callback methods
private
def store_referrer
session[:preauth_referrer] = request.referrer
end
end
然后,当用户从身份验证重定向返回时,您可以根据需要将引用者与持久存储中的用户相关联。
作为脚注,我建议使用 rails route url helpers 而不是直接引用路径,所以像这样:
<%= link_to image_tag("google_plus_icon.jpg", :height => 50, :border => 0, :alt => 'Google', :title => 'Google', :class => 'loginImg'), user_omniauth_authorize_path(provider: :google) %>