Open Uysim opened 5 years ago
not sure if I am right, but I think you have to override the Devise controller action which causes the redirect. Please someone correct me if I am wrong
Having the same issue
I found solution. You need to override devise failture_app
. That is how you can make different behavior for website and api
# lib/custom_failure.rb
class CustomFailure < Devise::FailureApp
def respond
if request.env['REQUEST_PATH'].start_with?('/api')
http_auth
else
redirect_to new_user_session_path
end
end
end
# config/initializers/devise.rb
config.warden do |manager|
manager.failure_app = CustomFailure
end
Do not forger to load lib folder in application.rb
config.autoload_paths << Rails.root.join('lib')
I think maybe you need a fallback (just don't set it) and send a http header Accept: application/json
I have my controller
I expect to it to render json error. But it redirect me to /user/sign_in page of devise. And what kind of exception you raise?