![](/img/trans.png)
[英]How to set custom header for all requests that go through OmniAuth?
[英]With Omniauth, How to record all requests for authentication?
在我的應用中使用omniauth,讓用戶使用Google oAuth2進行身份驗證,我將該用戶重定向到:
/users/auth/google_oauth2
如果用戶批准該請求,則將調用AuthenticationsController#create。
使用AuthenticationsController#create-我可以添加事件跟蹤來記錄批准google auth的用戶數。 我所沒有的是我發送給批准的電話號碼,這意味着我沒有轉換率。
如何跟蹤在發出連接請求時點擊URL的人數。
一個令人討厭的解決方案是圍繞Strategy_request_call方法構建一個過濾器並在那里進行跟蹤。
在初始化器中:
OmniAuth::Strategy.class_eval do
def request_call_with_tracking
log :info, "Im running before the actual request_call"
Tracker.hit(name) #name will return the provider
request_call_without_tracking
end
alias_method_chain :request_call, :tracking
end
您可以使用OmniAuth設置階段來實現。 您可以將:setup
選項傳遞給OmniAuth提供程序,並提供一個過程,該過程將在執行身份驗證之前執行。 您可以在此proc中添加事件跟蹤。
因此,如果您有一些跟蹤器類,則可以執行以下操作:
use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'],
:setup => lambda { |env|
Tracker.track
}
end
有關更多信息,請查看Avdi Grimm關於該主題的精彩博客文章 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.