我目前正在从此链接使用Rails API的JWT身份验证: https://github.com/nsarno/knock 我仔细地听了她的指示。但是,我仍然无法使用我刚刚注册的用户名和密码登录。我可以注册没有错误,并告诉我这条消息:
Started POST "/users" for ::1 at 2016-12-20 22:28:32 -0500
Processing by UsersController#create as HTML
Parameters: {"user"=>{"email"=>"xxx@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
(0.4ms) BEGIN
SQL (0.8ms) INSERT INTO "users" ("email", "password_digest", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "zsy2053@gmail.com"], ["password_digest", "$2a$10$1UAPAV92l.pHhbT.L3BvfOP7ieuy9yU2cdWdwy9VsVnTHjaRECB0W"], ["created_at", 2016-12-21 03:28:32 UTC], ["updated_at", 2016-12-21 03:28:32 UTC]]
(28.7ms) COMMIT
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (1.42ms)
Completed 200 OK in 162ms (Views: 21.2ms | ActiveRecord: 34.3ms)
但是,如果我只是使用我注册的用户名和密码登录,则会出现此错误:
Started POST "/knock/user_token" for ::1 at 2016-12-20 22:29:02 -0500
ActionController::RoutingError (No route matches [POST] "/knock/user_token"):
这是routes.rb
Rails.application.routes.draw do
resources :cars
get '/users/current-user', to: "current_user#show"
resources :users
mount Knock::Engine => "/knock"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
这是rake路线的输出| grep knock
knock /knock Knock::Engine
auth_token POST /auth_token(.:format) knock/auth_token#create
这是routers.rb
的配置Rails.application.routes.draw do
post 'user_token' => 'user_token#create'
resources :cars
get '/users/current-user', to: "current_user#show"
resources :users
mount Knock::Engine => "/knock"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
我该如何解决这个问题?
答案 0 :(得分:0)
好的,我已经阅读了这个宝石的文档。
默认情况下没有这样的路线。安装此gem时,应为您的应用程序生成令牌控制器:
rails generate knock:token_controller user
输出此命令:
create app/controllers/user_token_controller.rb
route post 'user_token' => 'user_token#create'
如您所见,令牌控制器已创建,并且也为其路由。