Last active
November 24, 2022 12:31
-
-
Save Kukunin/19e4cd9061ca698d132aa9818a2f743b to your computer and use it in GitHub Desktop.
Ruby Grape API authorization with Pundit example, without any extra gems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyGrapeAPI < Grape::API | |
helpers Pundit | |
after { verify_authorized } | |
helpers do | |
def current_user | |
nil # your authentication mechanism | |
end | |
end | |
resources :users do | |
desc "Retrieves information about given user" | |
params do | |
requires :id, type: String, desc: 'User ID' | |
end | |
get ':id' do | |
user = User.find(params[:id]) | |
authorize user, :show? | |
present user, with: Entities::User | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I created an authorization helper:
Then I just include it: