Skip to content

Instantly share code, notes, and snippets.

@noahhendrix
Created December 22, 2011 09:38
Show Gist options
  • 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
@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