使用 omniauth facebook 指示意图

Indicating intent with omniauth facebook

我正在使用 omniauth-facebook gem 并想向 Facebook 传递一个字符串,指示用户是否正在尝试加入或登录,以便我可以在他们 return 到我的网站。

如果我能做这样的事情就好了:

= 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 表明我的意图,并让他们 return 回应他们的意图,以便我可以在需要时使用它?

没有使用 Devise,并且宁愿避免必须设置会话变量来执行此操作,Facebook 肯定有满足此需求的解决方案...

看起来这是可能的,我的代码确实有效。当我检查来自 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

所以rails拼出来的params hash不包含想要的信息,但是响应中确实包含一堆未签名的细节,这就是我想要的信息可以找到的地方。

值得注意的是您也可以拨打:

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

很酷。