【问题标题】:Create new user session without password in authlogic在 authlogic 中创建没有密码的新用户会话
【发布时间】:2012-05-25 12:38:47
【问题描述】:

我在我的 Rails 3.0.10 应用程序中使用 authlogic 和 omniauth 进行身份验证。 但是,当我从omniauth 提供程序收到回调时,我无法创建新的用户会话来登录用户。

other responses(甚至在authlogic docs)中,它说使用UserSession.create(@user, true) 应该能够创建和保持一个新会话。

但是,这对我不起作用。它仅在@user 在数据库中有密码时才有效(通过直接在数据库中插入密码)。

但是使用第三方身份验证提供程序时没有密码,因此我无法登录用户。

任何想法如何在 authlogic 中登录没有密码的用户?

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 authlogic omniauth


    【解决方案1】:

    你可以在你的 User 模型中做这样的事情:

    acts_as_authentic do |config|
      external = Proc.new { |r| r.externally_authenticated? }
    
      config.merge_validates_confirmation_of_password_field_options(:unless => external)
      config.merge_validates_length_of_password_confirmation_field_options(:unless => external)
      config.merge_validates_length_of_password_field_options(:unless => external)
    end
    

    externally_authenticated? 只是用户的一种方法,用于检查提供用户信息的内容,如果它是omniauth 提供者之一,则返回true。

    【讨论】:

    • 您如何检查用户是否通过omniauth 进行身份验证?即你用什么externally_authenticated?
    • 在配置中声明 external 并将其作为选项传递对我来说不起作用。我必须通过 externally_authenticated? 本身作为它工作的选项。方法实现是单行的!authentications.empty? or password.blank?
    猜你喜欢
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多