2017-03-09 40 views
3

我使用的是omniauth-facebook gem,想通過Facebook的一個字符串指示用戶是否嘗試加入或登錄,以便我可以在他們返回到我的網站時使用這些信息。用omniauth指示意圖facebook

這將是巨大的,如果我可以做這樣的事情:

= link_to 'Join with facebook', '/auth/facebook?intent=join' 
= link_to 'Login with facebook', '/auth/facebook?intent=login' 

如果有人想知道爲什麼我要做到這一點,這就是我打算用這些信息做:

# pseudo code 
- if authentication fails 
    - if user was trying to join using facebook 
    = return them to the signup url 
    - elsif user was trying to login using facebook 
    = return them to the login url 

那麼是否有可能向Facebook表示我的意圖,並讓他們將他們的回覆意圖返回給我,以便我可以在需要時使用它?

我使用設計,並寧願以避免設置會話變量,要做到這一點是,Facebook的肯定有這種需求的解決方案...

+1

我想你知道這一點,這是關於facebook-omniauth的文檔。我還沒有找到任何有關它的信息,但也許在Facebook上,如果你搜索oauth,你可以找到更多的信息 https://github.com/mkdynamic/omniauth-facebook –

+1

謝謝@FabrizioBertoglio,我想我已經找到了一個解決方案,即將寫出答案。 – stephenmurdoch

回答

1

看起來這是可能的,我代碼確實有效。當我檢查從Facebook的反應,我看到以下內容:

>> request.env['omniauth.auth']['intent'] 
=> nil 
# no dice 

>> params 
=> returns lots of stuff, but not the thing I want 
# worth a try 

>> request.env['omniauth.params'] 
=> {"intent"=>"login"} 
# bingo 

使導軌params哈希表放在一起不包含所需的信息,但響應確實包含了一堆無符號的細節,而這正是我想要的信息可以找到。

值得注意的,你也可以撥打:

>> request.env['omniauth.origin'] 
=> "http://localhost:3000/join" 

很酷。

+0

我使用相同的技巧,以改善與彈出驗證流:D http://stackoverflow.com/questions/4491433/turn-omniauth-facebook-login-into-a-popup/9592204#9592204 – Ashitaka

+0

不要忘記爲你的失敗行爲做同樣的事情,當用戶不接受登錄時會被調用。 – Ashitaka

+0

感謝您的鏈接和提示@Ashitaka – stephenmurdoch