Skip to content

Instantly share code, notes, and snippets.

@noahhendrix
Created December 22, 2011 09:38
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save noahhendrix/1509698 to your computer and use it in GitHub Desktop.
Save noahhendrix/1509698 to your computer and use it in GitHub Desktop.
Authenticating with Grape
module Todo
class API < Grape::API
use Rack::Session::Cookie
version 'v1', :format => :json
helpers do
def current_user
return nil if env['rack.session'][:user_id].nil?
@current_user ||= User.get(env['rack.session'][:user_id])
end
def current_user=(user)
env['rack.session'][:user_id] = user.id unless user
@current_user = user
end
end
resource '/user' do
post do
User.create(params['user'])
end
end
resource '/user' do
http_basic do |username, password|
current_user = User.authenticate(username, password)
end
get do
current_user
end
end
end
end
@take
Copy link

take commented May 29, 2013

@noahhendrix thx for this gist, why did u use http_basic auth? :O

and could u add the user.rb too ? :D

@millisami
Copy link

Yup, I too want to look at the implementation of the authenticate(username, password) method.
Especially what it returns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment