令牌认证在设计后即使在添加token_authenticatable设计后也不能正常工作 [英] Token authentication in devise not working even after adding token_authenticatable to devise
问题描述
我有设计认证,以下是我做的步骤,令牌身份验证不起作用
- 添加
设计
到宝石文件 - rails generate devise:install
- rails generate devise User
- rake db:migrate
- rails generate devise:views
- rails generate controller home index
,直到现在没有问题,然后我添加了 devise:token_authenticatable
给用户模型,然后我添加了$ code> before_save:ensure_authentication_token 然后
- rails生成迁移AddTokenAuthenticatableToUser authentication_token:string
然后添加attr_accessible:authentication_token到用户模型
但
curl -H'Content-Type:application / json'-H'Accept:application / json'-X POST http:// localhost:3000 / users / sign_in -d{用户':{'email':'sample@gmail.com','password':'xxxxxxxx'}}
不工作。它在终端显示错误。请告诉我我应该怎么做才能获得用户的令牌并在随后的请求中使用它?
更新:
当从网络应用程序创建新用户时,它将身份验证令牌保存在用户表中,但无法从终端注册新用户,或获取用户使用令牌进行身份验证的令牌。
看到这个视频为devise,这可能对你有帮助,
一步一步如何设计作品
http:// railscasts.com/episodes/209-introducing-devise
I have devise for authentication and the following are the steps i did and token authentication not working
- added
devise
to gem file - rails generate devise:install
- rails generate devise User
- rake db:migrate
- rails generate devise:views
- rails generate controller home index
and upto this there is no problem and then i added devise:token_authenticatable
to user model and then i added before_save :ensure_authentication_token
and then
- rails generate migration AddTokenAuthenticatableToUser authentication_token:string
then added attr_accessible :authentication_token to the user model
but
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/users/sign_in -d "{'user' : { 'email' : 'sample@gmail.com', 'password' : 'xxxxxxxx'}}"
is not working. It displays error in terminal. pls tell me what should i do to get the token of user and use it in subsequent request?
UPDATE:
It saves authentication token in user table when creating new user from web app but could not register a new user from terminal or get the token for the user to authenticate using the token.
see this video for devise, this may helpful for you,
Step by Step, How devise works
http://railscasts.com/episodes/209-introducing-devise
这篇关于令牌认证在设计后即使在添加token_authenticatable设计后也不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!