How to get facebook login working with devise, omniauth - cannot overcome controller error

I am working on getting Facebook working with devise and omniauth. I have been trying to figure this out for days now without success. When I press the 'Facebook connect' button, the login page comes up. Once an FB email is entered along with a password, I am redirected to a page with the url suffix "{root domain}/users/sign_in#-" along with an error that says " translation missing: en.devise.authentications.user.failure." Users are not being added to the database. I have been at this for a while and it is very frustrating so far. I have routes as follows:

routes.rb

new_user_session GET    /users/sign_in(.:format)    sessions#new

user_session POST       /users/sign_in(.:format)    sessions#create

destroy_user_session DELETE /users/sign_out(.:format) sessions#destroy

user_omniauth_authorize /users/auth/:provider(.:format) authentications#passthru {:provider=>/facebook/}

user_omniauth_callback  /users/auth/:action/callback(.:format)  authentications#(?-mix:facebook)

In devise.rb I have:

config.omniauth :facebook, Facebook::APP_ID.to_s, Facebook::SECRET.to_s
require 'devise/orm/active_record'
config.authentication_keys = [ :email ]
config.http_authenticatable = false
config.http_authenticatable_on_xhr = false
config.navigational_formats = [:js, :html, :json]
config.token_authentication_key = :auth_token
config.sign_out_via = :delete

The config initializers omniauth.rb

if Rails.env == 'development' || Rails.env == 'test'
  Rails.application.config.middleware.use OmniAuth::Builder do
    provider :facebook, 'XXXXXX', 'XXXXXXXXXXXXX'
  end
else
  # Production
  Rails.application.config.middleware.use OmniAuth::Builder do
    provider :facebook, 'PRODUCTION_APP_ID', 'PRODUCTION_APP_SECRET'
  end
end

In users.rb I have:

   def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
        data = access_token.extra.raw_info
        if user = User.where(:email => data.email).first
            if user.facebook_id.nil?
                    user.facebook_id = data.id
                    user.save
            end
        user
        else # Create a user with a stub password. 
            token = Devise.friendly_token[0,20]
        User.create!(:email => data.email, :name=>data.name, :facebook_id=>data.id, :password => token, :password_confirmation=> token)
      end
    end

    def self.new_with_session(params, session)
       super.tap do |user|
         if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
             user.name = data["name"]
             user.email = data["email"]
             user.facebook_id = session["devise.facebook_data"]["extra"]["raw_info"]['id']
         end
       end
    end

In authentications controller (which is derived from the omniauth callbacks controller)

class AuthenticationsController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model
    @user = User.find_for_facebook_oauth(request.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"] = request.env["omniauth.auth"]
      flash[:notice] = "Please register an account first!"
      redirect_to "/"
    end
  end
end

In the development log, there is :

Started GET "/users/auth/facebook" for 127.0.0.1 at 2013-01-22 20:09:28 -0500

Started GET "/users/auth/facebook/callback? state=blah&code=blah" for 127.0.0.1 at     
2013-01-22 20:09:29 -0500
Processing by AuthenticationsController#failure as HTML
Parameters: {"state"=>"blah", "code"=>"blah"}
Redirected to http://myapp.com/users/sign_in
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)

On the Facebook app settings page, I have :

App Domain: (empty)

Site URL : http: //myapp.com/users/auth/facebook/callback?

and sandbox mode is enabled. I would appreciate any help at all in this. I have tried everything possible and check many sites, but I'm at a block.

Answers


There is a Devise issue on something like your immediate error message (found by web searching the "en.devise.authentications" part).

The Rails Internationalization (I18n) API guide describes the translations load path.

Apparently, here, Devise's lib/en.yml hasn't supplied the English string that Devise needs, but you can supply it yourself.

You haven't given us the contents of your config/locales/en.yml file, but that's where you can add:

en:
  devise:
    authentications:
      user:
        failure: User authentication failure

You can change the failure message to whatever you want.

See this example for the general i18n YAML format.

That should answer your question and solve your immediate error message.


Need Your Help

How to convert java.util.date to java.sql.date?

java sql datetime date

I am trying to use java.util.Date as input and then creating a query with it - so I need java.sql.Date.

How to start Qt development on Windows, but targeting Maemo 5?

windows qt nokia maemo qt-maemo

I've recently heard about Qt and read about how fun it is to develop with. By the way, I'm also buying a Nokia N900 and I heard that its operating system, Maemo 5, supports Qt quite nicely, so I th...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.