2013-08-28 2 views
1

Omniauth_callbacks_controller.rb? 페이스 북의 omniauth와 |

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

없음 문제, 아니 로그를 고안. 하지만 누군가 페이스 북을 통해 Connect하려고하면 기본 등록 양식으로 간단하게 던집니다. 관련된 다른 정보가 있기 때문에 이유

http://localhost:3000/sign_up#_=_ 

내 사용자 모델

class User < ActiveRecord::Base 

    devise :database_authenticatable, :registerable, #:recoverable 
     :rememberable, :trackable, :validatable, :omniauthable, 
       :omniauth_providers => [:facebook] 


    # Facebook Settings 
    def self.find_for_facebook_oauth(auth, signed_in_resource = nil) 
     user = User.where(provider: auth.provider, uid: auth.uid).first 
     if user.present? 
      user 
     else 
      user = User.create(first_name:auth.extra.raw_info.first_name, 
               last_name:auth.extra.raw_info.last_name, 
               facebook_link:auth.extra.raw_info.link, 
               provider:auth.provider, 
               uid:auth.uid, 
               email:auth.info.email, 
               password:Devise.friendly_token[0,20]) 
     end 
    end 



    validates :first_name, :presence => true 
    validates :email, :presence => true 
    validates :user_name, :presence => true, :uniqueness => true 

    has_many :clips 
    has_one :show, dependent: :destroy 

    # Profile Page Avatar 
    has_attached_file :avatar, styles: { avatar: "64x64#"}, 
             :default_url => "http://placehold.it/150x150&text=Missing" 

    validates_attachment :avatar, 
              content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }, 
              size: { less_than: 5.megabytes } 

    # Profile Page Cover 
    has_attached_file :profile_cover, styles: { cover: "870x150#"}, 
             :default_url => "http://placehold.it/150x150&text=Missing" 

    validates_attachment :profile_cover, 
              content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }, 
              size: { less_than: 5.megabytes } 


    # For Using the username instead of ID in the Link 
    def to_param 
     user_name 
    end 

end 
+0

'User.find_for_facebook_oauth'을 표시하십시오 –

+0

어디서 찾을 수 있습니까? –

+0

그냥 다음과 같이하십시오 : https://github.com/plataformatec/devise/wiki/OmniAuth:- 개요 –

답변

2

아니에요 100 % 확인합니다. 이것은 다른 가능한 효과를 제거하는 것입니다

@user ||= User.find_for_facebook_oauth ... 

@user = User.find_for_facebook_oauth 

  1. 변경이 라인, 그리고 @user를 캐시하는 작은 지점이있다 :

    내가 제안 이 작업은 자주 발생하지 않습니다.

  2. provider, uid, name 열이 추가되고 마이그레이션되었는지 확인하십시오.

  3. 이유는 @user가 유지되지 않으므로 사용자를 만들 때 잘못 될 수 있습니다. createcreate!으로 변경하면 발생한 오류를 확인할 수 있습니다.

    여전히 확실하지 않은 경우이 사용자 방법으로 디버그하십시오. 좋아

업데이트

는 이유를 얻었다. 당신은 검증이 :

validates :user_name, :presence => true, :uniqueness => true 

을하지만 페이스 북 속성에, 거기에 그런 속성 :user_name 없기 때문에 실패합니다 저장.

이 속성을 Facebook 해시에 추가하거나이 attr의 유효성을 검사하지 않는 방법이 있습니다.

+0

Yeap 이것은 문제입니다 :) Billy에게 감사드립니다. 내 애플 리케이션에서 내가 필요 : user_name 그 user_name 그 user_name을 입력하라는 메시지를 어떻게 설정할 수 있습니까? –

+0

@TheMiniJohn, Facebook 해시에는 user_name으로 사용할 수있는 "auth.extra.raw_info.name"필드가 있으며 다시 입력 할 필요가 없습니다. 페이스 북이 이미이를 검증했기 때문에 그들은 독특 할 것이다. 그러나이 필드는 기존 사용자의 중복을 충족시킬 수 있습니다. 일을 더 단순하게 만들기 위해이 attr의 고유성 검증을 제거 할 수 있습니다. 복잡한 해결책은'create!'중복 만나는 경우 몇 가지 고유 한 문자열을 추가함으로써, 그러나 이것은 또 다른 주제입니다. –

+0

"auth.extra.raw_info.name"지금은 해결되었지만이 방법을 사용해야합니다. 나는 user_name이 또한 User 's Profile에 대한 링크에서 호출되기 때문에 유일성이 필요하다. 사용자 등록 프로세스를 분할하는 방법을 찾고 있으므로, Facebook 연결 후에 user_name을 편집 할 수 있습니다. 하지만 고마워. :) –