1

I am using the grape_rails gem to manage the APIs and on my endpoints I have the following:

..api/v4/endpoints/base.rb

class Endpoints::V4::Base < Endpoints::Base
  before { authenticate_with_token! }

  version 'v4', using: :path

  mount Endpoints::V4::Menu
  mount Endpoints::V4::Customer
  mount Endpoints::V4::Orders   
end

I would like to skip the authenticate_with_token! method to Endpoints::V4::Menu in my base.rb file, but it's not working for me.

I have already tried with:

class Endpoints::V4::Base < Endpoints::Base
  skip_before { authenticate_with_token! only: :customer  } # first test 
  before { authenticate_with_token! except: :customer } # second test

  ...

  def customer
    mount Endpoints::V4::Products
  end
end

Thanks for your time

Samuel D.
  • 199
  • 10

1 Answers1

0

You can early return from your authentication method, only authenticate if some conditions are true.

class Endpoints::V4::Base < Endpoints::Base
  before { authenticate_with_token! } 

  ...

  def authenticate_with_token!
    return if <condition>

    ....
  end
end

or call the authentication method based on some condition

class Endpoints::V4::Base < Endpoints::Base
  before { authenticate_with_token! if authentication_required? } 

  ...

  def authenticate_with_token!
    
  end

  def authentication_required?
    <condition>
  end
end
Imam
  • 997
  • 7
  • 13