omn​​iauth-identity auth_key

时间:2012-07-17 15:01:16

标签: ruby-on-rails omniauth

我正在尝试将omniauth-identity集成到我的应用程序中。根据README文件,我们只需要写:

class Identity < OmniAuth::Identity::Models::ActiveRecord
  # Add whatever you like! 
end

但是我想添加唯一性验证。所以简单的方法是将此验证添加到Identity类:

validates_uniqueness_of :email, :case_sensitive => false

但是当我浏览gem源代码时,我在auth_key=中看到了OmniAuth::Identity::Models::ActiveRecord方法,如下所示:

    def self.auth_key=(key)
      super
      validates_uniqueness_of key, :case_sensitive => false
    end

因为我讨厌重复的代码,我想使用现有的方法而不是写一个额外的行。所以我将Identity类更改为

class Identity < OmniAuth::Identity::Models::ActiveRecord
  # Add whatever you like! 
  auth_key :email
end

但我仍然遇到重复的电子邮件(看起来验证不起作用)。因此,我尝试了以下Identity.auth_key = 'my_key',它为#`

给了我错误NoMethodError: super: no superclass method auth_key ='

知道我在这里做错了吗?当然我可以在OmniAuth :: Identity :: Models :: ActiveRecord中将auth_key =方法定义更改为auth_key,但我讨厌这样做,因为我觉得我在这里遗漏了一些东西。

谢谢

1 个答案:

答案 0 :(得分:3)

你差不多......你提供Identity类并继承自OmniAuth::Identity::Models::ActiveRecord,并指定OmniAuth Identity应该用来定位记录的列,只需使用{{1} setter方法。任何验证都应该包含在您的auth_key课程中,就像通常使用任何其他Identity模式一样。

ActiveRecord只是您选择在进行身份验证时找到记录的列的getter / setter(虚拟属性),它本身不是列,除非您选择创建身份模型中的auth_key列。

另请注意,OmniAuth Identity寻求的默认方法是auth_key属性(https://github.com/intridea/omniauth-identity/blob/master/lib/omniauth/identity/model.rb#L41),因此如果您选择坚持#email,设置auth_key是多余的属性。

#email

如果您决定将auth_key列更改为其他内容,例如# app/models/identity.rb class Identity < OmniAuth::Identity::Models::ActiveRecord belongs_to :user attr_accessible :email, :password, :password_confirmation, :user_id validates :email, :presence => true, :uniqueness => true, :case_sensitive => false validates :password, :presence => true, :confirmation => true validates :password_confirmation, :presence => true end # db/migrate/xxxxxxxxxxxxxx_create_identities.rb class CreateIdentities < ActiveRecord::Migration def change create_table :identities, :force => true do |t| t.column :email, :string, :null => false t.column :password_digest, :string t.column :user_id, :integer, :null => false end change_table :identities do |t| t.index :email, { :unique => true } t.index :user_id end end end # config/initializers/omniauth.rb use OmniAuth::Builder do provider :identity, :fields => [:email] end ,则可以使用#username setter,如下所示:

auth_key

注意,# app/models/identity.rb class Identity < OmniAuth::Identity::Models::ActiveRecord auth_key 'username' belongs_to :user attr_accessible :password, :password_confirmation, :username, :user_id validates :password, :presence => true, :confirmation => true validates :password_confirmation, :presence => true validates :username, :presence => true, :uniqueness => true end # db/migrate/xxxxxxxxxxxxxx_create_identities.rb class CreateIdentities < ActiveRecord::Migration def change create_table :identities, :force => true do |t| t.column :password_digest, :string t.column :username, :string, :null => false t.column :user_id, :integer, :null => false end change_table :identities do |t| t.index :username, { :unique => true } t.index :user_id end end end # config/initializers/omniauth.rb use OmniAuth::Builder do provider :identity, :fields => [:username] end 方法接受字符串参数,而不是像auth_key这样的符号。

OmniAuth Identity非常灵活,您可以利用其他几种自定义来适应现有项目。您可以为身份模型设置自定义类,并且可以自定义在进行身份验证时找到匹配记录的方式。请参阅https://github.com/intridea/omniauth-identity/blob/master/README.markdown

我希望这一切都有所帮助,我知道它让我困惑了一段时间,我不得不深入研究并理解OmniAuth Identity源代码。