令牌认证在设计后即使在添加token_authenticatable设计后也不能正常工作 [英] Token authentication in devise not working even after adding token_authenticatable to devise

查看:111
本文介绍了令牌认证在设计后即使在添加token_authenticatable设计后也不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有设计认证,以下是我做的步骤,令牌身份验证不起作用


  1. 添加设计到宝石文件

  2. rails generate devise:install

  3. rails generate devise User

  4. rake db:migrate

  5. rails generate devise:views

  6. rails generate controller home index

,直到现在没有问题,然后我添加了 devise:token_authenticatable 给用户模型,然后我添加了$ code> before_save:ensure_authentication_token 然后


  1. 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

  1. added devise to gem file
  2. rails generate devise:install
  3. rails generate devise User
  4. rake db:migrate
  5. rails generate devise:views
  6. 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_tokenand then

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆