grape icon indicating copy to clipboard operation
grape copied to clipboard

before_filter

Open leondu opened this issue 15 years ago • 14 comments

does Grape provide a mechanism like before_filter in rails controller?

say:

resources :missions do
  before_filter :authenticate!, :only => [:post]

  post do
    create mission here...
  end

  get do
    return all missions here...
  end
end

leondu avatar Jul 09 '11 06:07 leondu

You can't specify :only or :except, but Grape does have a universal before mechanism:

class Something < Grape::API
  before do
    # before code
  end

  get '/awesome' do
    # the 'before' code will run before this code
  end
end

mbleigh avatar Jul 15 '11 03:07 mbleigh

As far as I can tell, the before method doesn't exist. I have started to implement the functionality on my own and would love to contribute it. Am I doing something wrong or does the above code not work (as in: https://github.com/intridea/grape/issues/58)?

bmorton avatar Aug 25 '11 19:08 bmorton

If you really need to have a before filter only for certain requests, how is that done ?

letronje avatar Feb 18 '14 09:02 letronje

@letronje If I am not mistaken these are scoped to the API class, and you could mount multiple of these (I could be wrong, would be nice to extend the documentation either way).

dblock avatar Feb 18 '14 15:02 dblock

I have same question as @letronje ...How on earth do you only require authentication for certain requests? It's super easy with before_filter, but I can't figure it out for the life of me with this.

rubencodes avatar Jun 06 '14 19:06 rubencodes

Did you try my response above? I mean write two classes, AuthenticatedRequestsAPI and UnauthenticatedRequestsAPI, mount them into the same API. Only one would have a before filter.

dblock avatar Jun 06 '14 19:06 dblock

@dblock Can you provide an example? @rubencodes @letronje @leondu Did you ever figure this out?

blakeperdue avatar Apr 19 '15 02:04 blakeperdue

I'd like to set locale for all actions, how can I do that with all API? I tried with BaseAPI, and define like this:

class BaseAPI < Grape::API
  before do
    I18n.locale = env['rack.session'][:lang]
  end
end

then others will be extended from this class, but above snippet is never run

tamvm avatar May 07 '15 03:05 tamvm

@tam-vo It should. Try this in the actual API class, I imagine that will work. Then try again in the base class, if it doesn't work open a new bug please (with a test would be nice).

dblock avatar May 07 '15 11:05 dblock

I wrote two classes for auth and none-auth. But it does not work, please have a look what's wrong with it. The root Class:

class API < Grape::API
  prefix 'api'
  format :json

  rescue_from :all

  mount AuthLessAPI
  mount AuthRequiredAPI
end

The auth required class

class AuthRequiredAPI < Grape::API

  before do
    error!("401 Unauthorized, 401") unless authenticated
  end

  mount UserAPI
  mount ...
end

The auth less class

class AuthLessAPI < Grape::API
  ... some api ...
end

The API in the UserAPI does not required authontication.

birbird avatar Jul 17 '15 08:07 birbird

@birbird You'll have to provide a lot more detail on "it doesn't work". I suggest opening a new issue and making code that clearly demonstrates the problem.

dblock avatar Jul 17 '15 18:07 dblock

is there any way to scope the filter like "before" till some extent, not for all the post/get requests?

Pranow-Ojha avatar Jul 06 '17 06:07 Pranow-Ojha

Yes, see https://github.com/ruby-grape/grape#before-and-after.

dblock avatar Jul 06 '17 16:07 dblock

If anyone else comes here, I was having trouble getting callbacks to work in a base grape class that mounted others. The solution for me was to make sure the before/after callbacks are called before the mounting of the other classes are called.

Here's an example from our api:

module Api
  module V2
    class Base < Grape::API
      extend Api::V2::Documentation
      extend Api::V2::Authorization
      include ApiHelper

      after do # must be called before the mount calls below!
        track_request
      end

      mount Api::V2::Endpoints::Auth
      mount Api::V2::Endpoints::Users
    end
  end
end

synth avatar Sep 24 '17 19:09 synth