2017-03-09 41 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