38

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

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

3 Answers3

101

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! :)

fuzzyalej
  • 5,903
  • 2
  • 31
  • 49
  • 8
    Totally just saved my arse. Thank you sir. – Matt Darby Apr 17 '12 at 19:54
  • 2
    This would ideally work but there is a bug in the code! https://github.com/intridea/omniauth/issues/543 So, I just assigned the value to my session, and cleared it on callback :) – whizcreed Apr 30 '12 at 19:11
  • 1
    I'm using it, not problem whatsoever! (1.1.0) – fuzzyalej May 03 '12 at 07:49
  • 1
    What are you referring to by "in the callback"? – Syl Feb 16 '15 at 16:48
  • 1
    Additional Information: 1. The link with path `omniauth_authorize_path` calls the method `def facebook` in `Users::OmniauthCallbacksController` which calls another method `from_omniauth(request.env["omniauth.auth"])`. So in `def facebook` you can find them at `request.env["omniauth.params"]`, NOT inside the `["omniauth.auth"]`. If you want to use them in `from_omniauth`, you need to pass them to the method with `User.from_omniauth(request.env["omniauth.auth"], request.env["omniauth.params"])` – Fabrizio Bertoglio Mar 16 '17 at 16:15
9

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.

e3matheus
  • 2,112
  • 1
  • 20
  • 28
0

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.

Mateus
  • 67
  • 2