简体   繁体   English

如何通过设计验证令牌

[英]How to authenticate Token with devise

I want to use devise' token_authenticatable helper to authenticate users against the system. 我想使用devise的token_authenticatable帮助程序来针对系统对用户进行身份验证。

I found some older documentations where a method named valid_authentication_token?(...) is used but couldn't find the same in newer devise version. 我找到了一些较旧的文档,其中使用了一个名为valid_authentication_token?(...)的方法,但在较新的devise版本中找不到相同的方法。

So what's the right way to authenticate a user? 那么验证用户身份的正确方法是什么? Should I request the Model for user with named token and checking if email-adresses match? 我是否应该为具有命名令牌的用户请求模型并检查电子邮件地址是否匹配?

Thanks a lot for your help. 非常感谢你的帮助。

PascalTurbo PascalTurbo

If you add 如果添加

t.token_authenticatable

to you user ActionRecord, and add 您的用户ActionRecord,并添加

devise  :token_authenticatable

to your User model 到您的用户模型

and specify which param is your token key in config/initializer/devise, something like this: 并在config / initializer / devise中指定哪个参数是您的令牌键,如下所示:

config.token_authentication_key = :auth_token

then controllers that use 然后使用

before_filter :authenticate_user!  # Tell devise to use :user map 

to authenticate. 进行身份验证。 after the authenticate_user!, individual methods can test using 在authenticate_user!之后,可以使用

user_signed_in?

will authorize users either by the login session or the devise authorization token that is passed on the query string or passed using HTTP basic authentication. 将通过登录会话或通过查询字符串传递或使用HTTP基本身份验证传递的devise授权令牌对用户进行授权。 See Devise helper code for details. 有关详细信息,请参见Devise帮助程序代码

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

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