简体   繁体   English

Ruby on Rails:如何在会话中为authlogic存储其他数据

[英]Ruby on Rails: how can I store additional data in the session for authlogic

I'm using Authlogic to authenticate users. 我正在使用Authlogic对用户进行身份验证。

I understand how to create and use sessions, but want to store an additional id variable in the current_user session created by authlogic. 我了解如何创建和使用会话,但想在authlogic创建的current_user会话中存储其他id变量。

Can I just do something like this: 我可以做这样的事情吗:

 session[:authlogic_sess_name] = @extra_id.id 

However, I'm not sure what the authlogic session is named though or how to access it. 但是,我不确定authlogic会话的名称是什么或如何访问它。

Thanks! 谢谢!

Why would you not just store the value in the session? 您为什么不只将值存储在会话中?

session[:extra] = @extra_id.id

The Authlogic current_user is simply a value in the current session, managed by the Rails stack itself. Authlogic current_user只是当前会话中的一个值,由Rails堆栈本身管理。

I agree with Toby, if you don't need this additional attribute to be on AuthLogic's UserSession object, then it may be simpler to store the value in the session hash itself. 我同意Toby的观点,如果不需要在AuthLogic的UserSession对象上使用此附加属性,则将值存储在会话散列本身中可能会更简单。

But in my specific case, and perhaps in yours too, I have information that I want on the UserSession record specifically rather than the session hash, because I want to access it in a callback in the UserSession model, where "session" is unavailable. 但是在我的特定情况下,也许在您自己的情况下,我有特定于UserSession记录而不是会话哈希的信息,因为我想在UserSession模型中的回调中访问它,其中“会话”不可用。

Here is an older blog post describing how to store an additional attribute on UserSession: http://railsblog.kieser.net/2010/03/authlogic-custom-logins-and-persisting.html 这是一个较旧的博客文章,描述了如何在UserSession上存储其他属性: http: //railsblog.kieser.net/2010/03/authlogic-custom-logins-and-persisting.html

EDIT: I didn't have much luck with this approach myself. 编辑:我自己这种方法并没有太多运气。 To do the things that required information from the session hash, I put that logic in the controller instead of the model. 为了完成需要从会话哈希中获取信息的事情,我将逻辑放在控制器中而不是模型中。

AuthLogic actually has certain callbacks that execute controller methods. AuthLogic实际上具有执行控制器方法的某些回调。 For instance, AuthLogic will call last_request_update_allowed? 例如,AuthLogic将调用last_request_update_allowed? if your controller responds to that, right before setting last_request_at. 如果您的控制器对此做出了响应,则在设置last_request_at之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM