1

私はdeviseでomniauth-facebookを実装しましたが、すべてが完璧に機能しました。しかし、コーヒー スクリプト (Railscasts #360 から) を実装すると、コールバックが機能しません。私が見ているように、コントローラーをコールバックします。しかし、そのハッシュ認証。空です。

私が間違っていることはありますか?あなたはとても。

application.html.haml

....
-if user_signed_in?
          Logged in as
          = current_user.email
          =link_to 'Edit profile', edit_user_registration_path
          |
          =link_to "Logout", destroy_user_session_path, method: :delete
        -else
          =link_to "Sign up", new_user_registration_path
          |
          =link_to 'Log in', new_user_session_path
          |
          = link_to "Log in with Facebook", user_omniauth_authorize_path(:facebook), id: "sign_in" 
....

facebook.js.coffee.erb

jQuery ->
  $('body').prepend('<div id="fb-root"></div>')

  $.ajax
    url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js"
    dataType: 'script'
    cache: true


window.fbAsyncInit = ->
  FB.init(appId: '<%= ENV["FACEBOOK_APP_ID"] %>', cookie: true)

  $('#sign_in').click (e) ->
    e.preventDefault()
    FB.login (response) ->
      window.location = '/auth/facebook/callback' if response.authResponse

  $('#sign_out').click (e) ->
    FB.getLoginStatus (response) ->
      FB.logout() if response.authResponse
    true

ルート.rb

Hikultura::Application.routes.draw do
  devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
    :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :registrations => "registrations" }

  resources :events

  devise_scope :user do match 'auth/facebook/callback', to: 'users/omniauth_callbacks#facebook'
  end

  root :to => 'events#index'

users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)

    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable,
  # , :timeoutable and 
  devise :database_authenticatable, :registerable, :lockable,
         :recoverable, :rememberable, :validatable, :confirmable, :omniauthable

  attr_accessor :current_password

  # Setup accessible (or protected) attributes for your model
  attr_accessible :current_password, :email, :password, :password_confirmation, :remember_me, :provider, 
    :uid, :name, :gender, :town



  def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
    user = User.where(:provider => auth.provider, :uid => auth.uid).first || User.where(:email => auth.info.email).first

    unless user
      user = User.create(name:auth.extra.raw_info.name,
                           provider:auth.provider,
                           uid:auth.uid,
                           email:auth.info.email,
                           gender:auth.extra.raw_info.gender,
                           town:auth.extra.raw_info.location,
                           password:Devise.friendly_token[0,20]
                           )
      user.skip_confirmation!
    end
    user
  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.email = data["email"] if user.email.blank?
      end
    end
  end


end

コンソール サーバー ログ

NoMethodError (未定義のメソッドfind_for_facebook_oauth' app/controllers/users/omniauth_callbacks_controller.rb:5:`facebook' 内)provider' for nil:NilClass):
app/models/user.rb:32:in

4

0 に答える 0