您快到了...您提供Identity
类并继承自OmniAuth::Identity::Models::ActiveRecord
,并指定 OmniAuth Identity 应该用来定位记录的列,只需使用auth_key
setter 方法。任何验证都应包含在您的Identity
类中,就像它们通常与任何其他ActiveRecord
模型一样。
这auth_key
只是您选择在身份验证时定位记录的列的 getter/setter(虚拟属性),它本身不是列,除非您选择在身份模型中创建auth_key
列。
另请注意,OmniAuth Identity 寻求的默认方法是#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 Identity 非常灵活,您可以利用其他几个自定义项来适应现有项目。您可以为您的身份模型设置自定义类,并且可以自定义在身份验证时找到匹配记录的方式。请参阅https://github.com/intridea/omniauth-identity/blob/master/README.markdown。
我希望这一切都会有所帮助,我知道这让我困惑了一段时间,我不得不深入研究并理解 OmniAuth Identity 源代码。