你几乎没有......你提供Identity
类和OmniAuth::Identity::Models::ActiveRecord
继承,并指定列OmniAuth标识应使用定位记录,只需使用auth_key
setter方法。任何验证应包括在你的Identity
类,因为它们通常会与其他任何ActiveRecord
模型。
的auth_key
仅仅是您选择定位进行身份验证时记录的列的getter/setter(虚拟属性),它是不是列本身,除非您选择创建您的身份模型的auth_key
列。
还要注意,通过OmniAuth身份所寻求的默认方法是#email
属性(https://github.com/intridea/omniauth-identity/blob/master/lib/omniauth/identity/model.rb#L41)所以设置auth_key
是多余的,如果你选择坚持与#email
属性。
# 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
如果您决定更改AUTH_KEY列到别的东西,例如#username
,那么您需要使用auth_key
setter方法如下:
# 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
方法接受一个字符串参数和不是象attr_accessible
这样的符号。
OmniAuth身份是非常灵活,你可以充分利用,以满足现有项目其他几个自定义。您可以为您的身份模型设置自定义类,并且可以自定义在验证时找到匹配记录的方式。见https://github.com/intridea/omniauth-identity/blob/master/README.markdown。
我希望这一切可以帮助,我知道这让我困惑了一段时间,我不得不钻研和理解OmniAuth身份的源代码。
我的答案在下面有帮助吗? – Jurgen 2012-09-27 21:00:32