-
-
Save lgs/3061798 to your computer and use it in GitHub Desktop.
Authenticating with Grape
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment