设计身份验证令牌

时间:2011-12-14 02:44:44

标签: ruby-on-rails ruby devise gem

在设计中,我如何访问用户的身份验证令牌。当ajax调用启动用户会话时,我需要能够从用户那里获取身份验证令牌。我尝试过在我的模型中添加:token_authenticable

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

还添加名称属性,例如:authentication_token和:authenticity_token到attr_accessible。但是每当我尝试用user.auth调用authentication_token或者authenticity_token时,我会收到这样的错误

NoMethodError: undefined method `authentication_token' for #<User:0x000001016b1140>

抱歉,我需要通过我的ajax调用发送此令牌,(我工作的最后一个项目我有一个令人难以置信的BOOTLEG解决方案,其中设备几乎被从应用程序中删除,我只需要这个该死的令牌)

1 个答案:

答案 0 :(得分:8)

通过将:token_authenticatable模块包含到devise用户模型中来解决此问题:

devise :token_authenticatable, ...

此方法也是在类本身上定义的,而不是实例。将其称为User.authentication_token

见这里:https://github.com/plataformatec/devise/blob/master/lib/devise/models/token_authenticatable.rb#L61-63