Search code examples
ruby-on-rails-4oauth-2.0deviseomniauth

Is it possible to pass parameters to oauth?


I'm using Devise with the gems "omniauth-google-oauth2", "omniauth-linkedin-oauth2", and "omniauth-oauth2", and looking for a way to do something like this:

<%= link_to user_google_oauth2_omniauth_authorize_path(registerable: true) %>

... so that I could condition on the response (specifically, to allow users to register this way from one page, but not from another)

The ideal would be just that the query was passed back to me in the auth params, but any way I could affect the params returned to me would do. But I don't see anything in the readme for any of the gems suggesting it's possible.

As a possible workaround, I was thinking we could set a cookie or store a session value when they visit one of the relevant pages, and then check against (and delete) that when we receive the oauth request, but that seems very hacky.


Solution

  • OAuth supports the state paramete. You can add it like:

    <%= link_to user_google_oauth2_omniauth_authorize_path(state: "some_message") %>