How can I configure Devise for Ruby on Rails to store the emails and passwords somewhere other than in the user model?

ε祈祈猫儿з 提交于 2020-01-10 09:01:37

问题


I'd like to store emails in a separate table and allow users to save multiple emails and log in with any of them. I'd also like to store passwords in a different table.

How can I configure Devise to store authentication info elsewhere?

Worst case scenario, if I just have to hack into it, is there a generator to just port everything over to the app? I noticed there was a generator for the views.

Thanks.


回答1:


To do this, you have just to set up your models properly, create your email association. Also you have to override the user finder method from devise, to do this you have to override self.find_for_database_authentication(conditions) method.

class User < ActiveRecord::Base
  has_many :emails
  accepts_nested_attributes_for :emails
  ...
  def self.find_for_database_authentication(conditions)
    email = conditions.first
    self.where(:emails=>{:name=>email})
  end
end

Also you have to add the emails to your devise.rb to use it.

Devise.setup do |config|
  ...
  config.authentication_keys = [ :emails ]
  ...
end

I haven't ran the code, maybe it has some errors, but I hope you get the idea. Please let me know is something is not clear. Cheers.

Update

First question: Yes, the method from the devise will bee overrided automatically.

Second question: You should have your_application_path/config/initializers/devise.rb I think it's created when you use the devise gem. If you don't have you can create it. The content I specified is good, just omit the dots (...)



来源:https://stackoverflow.com/questions/4485993/how-can-i-configure-devise-for-ruby-on-rails-to-store-the-emails-and-passwords-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!