1

我用设计实现了omniauth-facebook,一切都很完美。但是,当我实现一些咖啡脚本(来自 Railscasts #360)时,我无法让回调工作。正如我所看到的,它回调控制器。但是那个哈希验证。是空的。

任何想法我做错了什么?那你很。

应用程序.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'

用户/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

用户.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:in `facebook'provider' for nil:NilClass):
app/models/user.rb:32:in

4

0 回答 0