Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Omniauth multiple facebook login strategy
# initializers/devise.rb
config.omniauth :facebook, [APP_ID], [APP_SECRET]
config.omniauth :facebook_app1, [APP_ID], [APP_SECRET], :iframe => true, :scope => 'publish_stream,offline_access,email'
config.omniauth :facebook_app2, [APP_ID], [APP_SECRET], :iframe => true, :scope => 'publish_stream,offline_access,email'
# Create multiples Omniauth facebook login in the same app
# See this post for more details
# http://wealsodocookies.com/posts/how-to-create-multiple-facebook-omniauth-strategies-for-the-same-application
# initializers/omniauth.rb
module OmniAuth::Strategies
class FacebookApp1 < Facebook
def name
:facebook_app2
end
end
class FacebookApp2 < Facebook
def name
:facebook_app2
end
end
end
# users/omniauthCallbacks.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def facebook_app1
@user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in @user, :event => :authentication
redirect_to "[FACEBOOK_APP1_URL]"
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def facebook_app2
@user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in @user, :event => :authentication
redirect_to "[FACEBOOK_APP2_URL]"
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment