如何用设计验证令牌

Pas*_*rbo 1 authentication ruby-on-rails token devise

我想使用设计'token_authenticatable帮助程序来对系统进行用户身份验证.

我找到了一些较旧的文档,其中使用了名为valid_authentication_token?(...)的方法,但在较新的设计版本中找不到相同的方法.

那么验证用户的正确方法是什么?我是否应该为具有命名令牌的用户请求Model并检查电子邮件地址是否匹配?

非常感谢你的帮助.

PascalTurbo

Ste*_*elm 7

如果你添加

t.token_authenticatable
Run Code Online (Sandbox Code Playgroud)

给你用户ActionRecord,并添加

devise  :token_authenticatable
Run Code Online (Sandbox Code Playgroud)

到您的用户模型

并在config/initializer/devise中指定哪个param是您的令牌密钥,如下所示:

config.token_authentication_key = :auth_token
Run Code Online (Sandbox Code Playgroud)

那么使用的控制器

before_filter :authenticate_user!  # Tell devise to use :user map 
Run Code Online (Sandbox Code Playgroud)

进行身份验证.在authenticate_user!之后,各个方法可以测试使用

user_signed_in?
Run Code Online (Sandbox Code Playgroud)

将通过登录会话或在查询字符串上传递或使用HTTP基本身份验证传递的设计授权令牌来授权用户.有关详细信息,请参阅设计助手代码.