【问题标题】:Not able to create session with an object无法与对象创建会话
【发布时间】:2023-03-15 14:35:02
【问题描述】:

我正在尝试在 ruby​​ on rails 中实现我的第一个基于 authlogic 的应用程序。我需要使用自定义 oauth2 流程对用户进行签名,在流程结束时,我想告诉 authlogic 我希望他对哪个用户进行身份验证。无需提供登录名或密码,只需直接传递用户对象即可。

我在 README 中发现我可以使用 UserSession.create(user, true) 做到这一点,但将其放在我的 OAuth 回调步骤的末尾并不会保留我的用户。没有错误也没有异常,一切似乎都很好,但UserSession.find 返回 nil(即使在UserSession.create 之后的下一行)。我在 Rails 5.1.6 和 authlogic 4.0.1 上。

用户模型

class User < ApplicationRecord
  acts_as_authentic
end

用户会话模型

class UserSession < Authlogic::Session::Base
end

提前感谢任何关于我做错了什么的线索。

【问题讨论】:

  • oauth 回调只是facebook 给你打电话controller#action 吗?如果是这样,您只是在该操作结束时添加了UserSession.create(user, true)
  • 这并不像我实际使用乳齿象 oauth 那样简单,但这是一个完全自定义的解决方案。重要的是,在控制器操作中,我知道我想要验证哪个用户,但我不想使用他的电子邮件/通行证来做到这一点。我只想直接认证他。为了简单起见 - 让我们假设我有一个非常愚蠢的操作,它从 DB 获取最后一个用户并将他登录 - UserSession.create(User.last, true)
  • 如果你这样做session = UserSession.create(user, true)put session.errors.full_messages,我们可以更好地理解这个问题
  • 我尝试调试错误但没有任何错误(session#&lt;UserSession: {:unauthorized_record=&gt;"&lt;protected&gt;"}&gt;)。顺便说一句 - 我的应用程序中唯一不标准的事情是我在表中没有电子邮件、登录名和密码字段。以防万一。
  • 我想阅读UserSession的源代码,但现在我正在阅读使用rails github.com/binarylogic/authlogic#2-rails设置authlogic的指令

标签: ruby-on-rails authlogic


【解决方案1】:

这是我发现的,来自Authlogic::Session::Base

module Authlogic
  module Session # :nodoc:
    # This is the most important class in Authlogic. You will inherit this class
    # for your own eg. `UserSession`.
    #
    # Code is organized topically. Each topic is represented by a module. So, to
    # learn about password-based authentication, read the `Password` module.
    #
    # It is common for methods (.initialize and #credentials=, for example) to
    # be implemented in multiple mixins. Those methods will call `super`, so the
    # order of `include`s here is important.
    #
    # Also, to fully understand such a method (like #credentials=) you will need
    # to mentally combine all of its definitions. This is perhaps the primary
    # disadvantage of topical organization using modules.

正如我从他们的说明中读到的,他们从来没有说您可以在没有电子邮件和密码的情况下验证用户,但您可以使用 User 对象创建会话

# skip authentication and log the user in directly, the true means "remember me"
UserSession.create(my_user_object, true)

我会阅读更多关于Session.rb的信息

https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/session/session.rb

抱歉,不能给你更多帮助

PS 你也可以通过在 gem 文件中设置 binding.pry 来调试这个 gem

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    相关资源
    最近更新 更多