How to make Devise authentication respond to JSON only?
module DeviseOverrides
class SessionsController < Devise::SessionsController
# Respond only to JSON calls
clear_respond_to
respond_to :json
end
end
You must clear_respond_to
(for clear all types previously defined, like HTML or XML) and then respond_to :json
in the override controller...
config/routes.rb
devise_for :users , controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations'
}
controllers/users/sessions
class Users::SessionsController < Devise::SessionsController
clear_respond_to
respond_to :json
end
controllers/users/registrations
class Users::/RegistrationsController < Devise::RegistrationsController
clear_respond_to
respond_to :json
end
It works in Rails 5.2 for me!
I imagine you could override the Devise Controllers:
In controllers/devise_overrides/sessions_controller.rb
:
class DeviseOverrides::SessionsController < Devise::SessionsController
respond_to :json
respond_to :html, only: []
respond_to :xml, only: []
end
In routes.rb
:
devise_for :users, controllers: {
sessions: "devise_overrides/sessions"
}