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.