How to create multiple facebook Omniauth strategies for the same application
In a previous post, I talked about testing Facebook login with capybara. Today I just want to share a quick “recipe” that I used to create multiple Facebook logins. Let me explain.
What is the problem?
In the next version of myDrinkaware, we have a fairly common Facebook login button.
Using Devise and Omniauth, this is defined in your initializer with:
config.omniauth :facebook, APP_API, APP_SECRET
But my problem was that I needed 3 different sorts of login! 1 for the website and 2 for Facebook apps, which are embedded in the code.
I couldn’t use the same login Strategy because I needed the :iframe option on the Facebook apps login (if you don’t put the iframe option, you’re going to run into the “Redirect to Facebook” problem). And obviously, depending on the login Strategy, the redirection will be different after the login.
The solution
I’ve created an omniauth.rb initializer like this one:
Then edit your devise.rb to reflect the new Facebook apps:
Finally, make sure that you have one callback method per new Strategy:
And now you should be able to login using:
Let me know if it has been useful ;)
Post written by Nicolas Alpi
Nicolas is a talented Ruby on Rails/javaScript developer. He manipulates his TypeMatrix keyboard like no one else, likes VIM, Linux, and above all loves finding pragmatic solutions to complex problems. When he is not developing a new web application, he likes reading, hiking and eating Nathalie's cookies.
You can also follow Nicolas on Twitter at @spyou.
If you have any questions contact him at nicolas@wealsodocookies.com or leave a comment.