Passing parameters through OmniAuth

synapse picture synapse · Mar 27, 2012 · Viewed 24.8k times · Source

I need to pass some parameters to callback action. Judging from the source code, OmniAuth should add query string to callback URL but strangely it does not. When I open

/auth/facebook?from=partner

...and get redirected to Facebook, return_url is just

/auth/facebook/callback

...without any parameters.

Answer

nfriend21 picture nfriend21 · Jul 12, 2013

After struggling with all the above answers, I figured out what to do regarding Facebook, which by default does not display the params in request.env["omniauth.auth"].

So -- If you are using a query string for the callback, similar to something like this:

"/auth/facebook?website_id=#{@website.id}"

The only way to get that website_id param is by using request.env["omniauth.params"]. NOTE: MAKE SURE YOU USE omniauth.params and not omniauth.auth -- this one tripped me up for a while.

Then, to test this out, you can inspect it within your controller action (notice the RAISE line...):

def create
  raise request.env["omniauth.params"].to_yaml 
  # the rest of your create action code...
end

You should see your parameter there. Great. Now, go back to your controller and remove that RAISE line. Then, you can access the param as follows in your controller action:

params = request.env["omniauth.params"]
website_id = params["website_id"]

NOTE: in params["website_id"] you need to use quotes and NOT a symbol.