Skip to content

Instantly share code, notes, and snippets.

@jaimeiniesta
Created March 1, 2012 14:48
Show Gist options
  • Select an option

  • Save jaimeiniesta/1950241 to your computer and use it in GitHub Desktop.

Select an option

Save jaimeiniesta/1950241 to your computer and use it in GitHub Desktop.
Grape API example
# This example API will recognize users that have authenticated via a web form,
# but it fails authenticating from http_basic_auth, which has been enabled in Devise for this app
# curl -v -u "[email protected]:herpassword" http://localhost:3000/api/my_email
module MyApp
class API < Grape::API
prefix 'api'
helpers do
def warden
env['warden']
end
def current_user
warden.user
end
def authenticate!
error!('401 Unauthorized', 401) unless current_user
end
end
get 'hello' do
{:hello => 'world'}
end
get 'my_email' do
authenticate!
current_user.email
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment