0

Using omniauth for facebook, I would like to pass some extra parameters to the URL in order to handle them on the callback.

I found a lot of documentation about this (in addition to the official documentation, this one, for example: Devise + Omniauth - How to pass extra parameters along?). But no one worked for me.

Using:

omniauth_authorize_path(:user, :facebook, msg: 'Hello')

produces this url:

/users/auth/facebook?locale=fr

Any idea of why it doesn't work? Thanks.

Update:

The parameter locale=fr is because I have asked the application to always use a locale in the URL. My application_controller.rb uses the following method:

class ApplicationController < ActionController::Base

    protect_from_forgery

    before_filter :set_locale

    def set_locale
        I18n.locale = params[:locale] || I18n.default_locale
    end

    # Always add :locale parameter to URL
    def url_options
        {:locale => I18n.locale}.merge(super)
    end

end

Removing url_options method doesn't solve the problem, and now gives the following URL:

/users/auth/facebook
Community
  • 1
  • 1
Hassen
  • 6,966
  • 13
  • 45
  • 65
  • where does `locale=fr` come from? Is it possible that this piece of code is overwriting previous variables? – Rajesh Kolappakam Sep 12 '13 at 07:18
  • This is because in my _application_controller.rb_ I have set the default locale to be always present in URL using a method. Deleting this method removes `locale=fr` but doesn't show the extra parameters. – Hassen Sep 12 '13 at 07:34

3 Answers3

2

Not entirely a lot of help, but I just tried this on my own code and it seemed to have worked fine:

omniauth_authorize_path(:user, :facebook, msg: "Hello")

produced

http://localhost:3000/users/auth/facebook?msg=Hello

I guess one interesting thing to note is that the first line is also equivalent to:

user_omniauth_authorize_path(:facebook, msg: "Hello")

Can I ask what version of the gems you are using?

The only other thing I can think about is perhaps your url_options method is interfering with devise. Try changing your set_locale method to look like this:

def set_locale
   I18n.locale = params[:locale] || I18n.default_locale
   self.default_url_options[:locale] = I18n.locale 
end
derekyau
  • 2,936
  • 1
  • 15
  • 14
  • Thanks @derekyau. Your solution helped me. using _user_omniauth..._ in addition to _set_locale_ is OK. – Hassen Sep 12 '13 at 08:37
0

you could not pass any random named param to the facebook auth and get it back at callback but one way to get around the problem is to use the param state /users/auth/facebook?state=fr but beware that state is by default used to prevent CSRF (http://en.wikipedia.org/wiki/Cross-site_request_forgery) so maybe you could complexify a little your state param with extra data to be sure that the callback is genuinly from your previous request

amine
  • 502
  • 5
  • 14
  • You mean using *state* name instead? like this: `omniauth_authorize_path(:user, :facebook, state: 'Hello')`. I tried, but still get the same URL: */users/auth/facebook* – Hassen Sep 12 '13 at 07:49
  • no i mean when you call /users/auth/facebook you pass it as a GET request attribute and in the callback you will get it back. the callback url would look like that: /users/auth/facebook/callback?code=somestring&state=fr so in your callback handler method you could use params['state'] – amine Sep 12 '13 at 08:08
0

Just recently solved that problem my self. The result is gem: https://github.com/kot-begemot/rails-localization

Yet have to release it to RubyGems, but forgot my credentials for it =(

Max
  • 2,063
  • 2
  • 17
  • 16