Skip to content

Route callback to controller action

fourcolors edited this page Mar 13, 2012 · 2 revisions

Disclaimer - This is only tested for Rails 3 and is very specific. I'm adding this because I felt many users were looking for a solution and although this isn't something that is built into omniauth, it's something I thought was useful for to know.

Say for example your are using the facebook omniauth and would like to direct users to a registration page if they have the "state" parameter set (maybe to an invitation token or something) and direct them to your sessions create page if the state parameter isn't set. You can do that like this.

module NewUserConstraint
  extend self

  def matches?( request )
    request.query_parameters["state"].present?
  end
end

module NewSessionConstraint
  extend self

  def matches?( request )
    request.query_parameters["state"].blank?
  end
end


YourApplication::Application.routes.draw do
  match "/auth/:provider/callback" => "sessions#new", :constraints => NewSessionConstraint
  match "/auth/:provider/callback" => "users#new", :constraints => NewUserConstraint

  root :to => 'invitations#new'
end

As you can see, you can use Rails Advanced Constraints to route based on params. Sources for this solution came from the following: