Skip to content

Instantly share code, notes, and snippets.

@mscoutermarsh
Created November 25, 2012 01:40
Show Gist options
  • Save mscoutermarsh/4142049 to your computer and use it in GitHub Desktop.
Save mscoutermarsh/4142049 to your computer and use it in GitHub Desktop.
Grape API authentication methods
# /api/auth
resource :auth do
desc "Creates and returns access_token if valid login"
params do
requires :login, :type => String, :desc => "Username or email address"
requires :password, :type => String, :desc => "Password"
end
post :login do
if params[:login].include?("@")
user = User.find_by_email(params[:login].downcase)
else
user = User.find_by_login(params[:login].downcase)
end
if user && user.authenticate(params[:password])
key = ApiKey.create(:user_id => user.id)
{:token => key.access_token}
else
error!('Unauthorized.', 401)
end
end
desc "Returns pong if logged in correctly"
params do
requires :token, :type => String, :desc => "Access token."
end
get :ping do
authenticate!
{ :message => "pong" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment