Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is that gem work with API? #348

Open
Uysim opened this issue Aug 16, 2019 · 4 comments
Open

Is that gem work with API? #348

Uysim opened this issue Aug 16, 2019 · 4 comments
Labels
support request This issue is a request for support using Simple Token Authentication.

Comments

@Uysim
Copy link

Uysim commented Aug 16, 2019

I have my controller

 class BaseController < ActionController::API
    acts_as_token_authentication_handler_for User, fallback: :exception
end 

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?

@exocode
Copy link

exocode commented Oct 22, 2019

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

@andreydro
Copy link

Having the same issue

@andreydro
Copy link

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')

@drihu
Copy link

drihu commented Jun 5, 2020

I think maybe you need a fallback (just don't set it) and send a http header Accept: application/json

@gonzalo-bulnes gonzalo-bulnes added the support request This issue is a request for support using Simple Token Authentication. label Jan 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support request This issue is a request for support using Simple Token Authentication.
Projects
None yet
Development

No branches or pull requests

5 participants