2

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.

4

1 に答える 1

2

あなたの即時のエラーメッセージのようなものにDeviseの問題があります(「en.devise.authentications」の部分をウェブで検索すると見つかります)。

Rails Internationalization (I18n) API ガイドでは、翻訳のロード パスについて説明しています。

どうやらここでは、Devise の lib/en.yml は Devise が必要とする英語の文字列を提供していませんが、自分で提供することができます。

config/locales/en.yml ファイルの内容を提供していませんが、そこに以下を追加できます。

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

失敗メッセージは任意のものに変更できます。

一般的な i18n YAML 形式については、この例を参照してください。

それはあなたの質問に答え、あなたの即時のエラーメッセージを解決するはずです.

于 2013-02-01T20:55:39.443 に答える