I'm currently using the JWT authentication for Rails API from this link: https://github.com/nsarno/knock and I followed her instruction carefully. However, I still cannot login with the username and password I just registered. I can register with no error, and show me this message:
Started POST "/users" for ::1 at 2016-12-20 22:28:32 -0500
Processing by UsersController#create as HTML
Parameters: {"user"=>{"email"=>"[email protected]", "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", "[email protected]"], ["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)
However, I got this error if I just login with the user name and password I registered:
Started POST "/knock/user_token" for ::1 at 2016-12-20 22:29:02 -0500
ActionController::RoutingError (No route matches [POST] "/knock/user_token"):
Here is the 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
Here is the output of rake routes | grep knock
knock /knock Knock::Engine
auth_token POST /auth_token(.:format) knock/auth_token#create
Here is the configuration of 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
How can I solve this problem?
Ok, I have read docs for this gem.
There is no such route by default. When you installing this gem, you should generate token controller for your application:
rails generate knock:token_controller user
Output of this command:
As you can see, token controller was created, and also route for it.