Devise + Omniauth - How to pass extra parameters a

2019-01-07 06:48发布

I have the ability in the app to load /users/auth/facebook to connect to facebook. I want to be able to know where the request came from. Whether it was from a user who is registering with facebook, or a existing user who simply wants to connect with facebook. Based on the type, the responses are very different.

How can I pass a param along to omniauth when authenticating. I tried doing:

/users/auth/facebook?connect_action=signup_connect_to_facebook

But that connect_action param didn't make it when it hit AuthenticationsController#Create

Ideas? Thansk

3条回答
我命由我不由天
2楼-- · 2019-01-07 07:00

For facebook use the 'state' parameter and pass what you want, but don't forget the encode.

/users/auth/facebook?state=parameter

You could send the parameter as a url form encoded json and then in the callback parse it.

查看更多
欢心
3楼-- · 2019-01-07 07:06

If the request is made from different pages in your application, you can examine the request.env['omniauth.origin']. Omniauth saves this variable automatically.

Here is a more detailed explanation

As far as passing custom parameters, I have tried to do this unsuccessfully. The workaround is to store it in the session before going to the provider as explained here.

Hope this helps you.

查看更多
男人必须洒脱
4楼-- · 2019-01-07 07:11

You have to use the :params options, as in

omniauth_authorize_path(:user, :facebook, var: 'value', var2: 'value2' )

and later in the callback you can access request.env['omniauth.params'] to get the hash! :)

查看更多
登录 后发表回答