2013-08-28 89 views
1

Omniauth_callbacks_controller.rbif @ user.persisted?与Facebook 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 

没有问题,没有日志。但是当有人试图通过Facebook进行连接时,它只会将其引导至默认的注册表单。

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:-Overview –

回答

2

我不是100%肯定,因为有一些涉及其他信息的原因。

我建议:

  1. 改变这一行

    @user ||= User.find_for_facebook_oauth ... 
    

    @user = User.find_for_facebook_oauth 
    

    这是为了消除其他可能的影响,并有小点缓存@user因为这一行动不会经常发生。

  2. 确保您已添加并迁移了provider,uid,name列。

  3. 原因应该是@user不会被保留,因此创建用户时可能会出现问题。您可以将create更改为create!以查看抛出的错误。

    如果仍然不确定,请调试此用户方法。

更新

好,知道了原因。你有一个验证:

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

但在Facebook的属性,有没有这样的属性:user_name,所以节约会失败。

修复只是使用一种方法将此属性添加到Facebook散列,或不验证此属性。

+0

Yeap这是问题:)谢谢比利。在我的应用程序中我需要:user_name如何设置该用户的提示输入该用户名? –

+0

@TheMiniJohn,在Facebook哈希中有一个可以用作user_name的字段“auth.extra.raw_info.name”,无需再次输入。他们将是独一无二的,因为Facebook已经验证了他们。但是这个字段仍然有可能在现有用户中遇到重复。为了使事情更简单,您可以删除此属性的唯一性验证。一个复杂的解决方案是拯救'创造!'如果重复相遇,则附加一些唯一的字符串,但这是另一个话题。 –

+0

“auth.extra.raw_info.name”现在工作,但我需要一种方法。我需要唯一性,因为:user_name也在指向用户配置文件的链接中调用。 我正在搜索如何拆分用户注册Proccess,因此:user_name可以在Facebook Connect之后进行编辑。但谢谢:) –