omniauth-facebook + rails 3 + authlogic debug

Personally I prefer Devise to Authlogic but recently I have to work with Authlogic on a legacy codebase. But I still like Devise because it’s up-to-date, clean, easier to use…

omniauth-facebook provides a cool way to login with FB – display a popup instead of redirecting users to FB login page then back to our site. Obviously this is the way we want :)

We won’t re-invent the wheel here, let’s follow Railcasts #360 to have omniauth-facebook integrated to your app. These are some issues you may encounter:

1.  Can’t log users in manually with Authlogic:

In some cases the user can’t be logged in. For me, the reasons were:

  • validations failed (lack/duplicated of email/username…): some FB users set their emails to private so ommiauth-facebook can’t get their emails, you have to set a fake email in order to pass the email validation, therefore the new user can be created in database and gets logged in.NOTES: the first time users login with their FB accounts, their accounts will be saved to database and they will be logged in automatically, we just have to manually log FB users in only when the FB accounts are already created in our database. = || "fb_user_#{}"
  • password not present: Devise provides a method Devise.friendly_token ( detailed guide here ) to manually create a password  but Authlogic doesn’t. You have to provide password and password_confirmation manually, make sure they’re the same.
  • create a new UserSession in controller: to log user in manually, we do
    UserSession.create(my_user_object, true)

    I’m not sure why but user won’t get logged in if the above is called in controller, moved it to model works.

2.  Invalid credentials

you see something like this in the log console:

(facebook) Authentication failure! invalid_credentials: OmniAuth::Strategies::OAuth2::CallbackError, OmniAuth::Strategies::OAuth2::CallbackError

If you remove the JS code you won’t get this anymore, but the popup will be gone as well. It seems this is a bug of omniauth-facebook 1.4.1 (the newest version at the time of this writing), let’s use omniauth-facebook 1.4.0 instead, everything works just fine then.

3.  FB errors

You’ll may get some error messages while trying to login with FB, make sure in your FB app configuration, Sandbox Mode is set to Disabled.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s