【问题标题】:authlogic email as usernameauthlogic 电子邮件作为用户名
【发布时间】:2010-03-17 23:53:13
【问题描述】:

我如何覆盖/设置 authlogic 以使用电子邮件字段而不是用户名字段进行注册和身份验证,对于某些注册场景来说,用户名 + 电子邮件有时过于密集

【问题讨论】:

    标签: ruby-on-rails ruby authlogic


    【解决方案1】:

    如果您只是删除 login 列并添加一个 email 列,剩下的工作将由 authlogic 完成。

    请参阅此示例readme,了解所有可选/必需的数据库列。

    【讨论】:

    • 神奇!。我还发现使用它也有效:acts_as_authentic do |c| c.login_field = :email 结束
    • 我需要使用电子邮件登录,但要零售用户名。所以我更喜欢ADAM的评论回答。
    【解决方案2】:

    更好的答案试试这个...好吧,如果需要,更新 authlogic gem!

    user_session.rb

    class UserSession < Authlogic::Session::Base
      find_by_login_method :find_by_email #for example or you can make what ever method see exapmle 2
    end
    

    --- 示例 2

    user_session.rb

    class UserSession < Authlogic::Session::Base
      find_by_login_method :find_by_anything
    end
    

    用户.rb

    class User < ActiveRecord::Base
      acts_as_authentic
    
      def self.find_by_anything(login)
        find_by_login(login) || find_by_email(login) || find_by_id(login)
      end
    end
    

    【讨论】:

      【解决方案3】:

      正如亚当的评论包含对这个问题的一个很好的答案。将此添加到您的用户模型中:

      class User < ActiveRecord::Base
        acts_as_authentic do |c| c.login_field = :email end
      end
      

      【讨论】:

        猜你喜欢
        • 2015-04-27
        • 2012-02-06
        • 2011-09-15
        • 2011-04-02
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        相关资源
        最近更新 更多