Created
November 25, 2012 01:40
-
-
Save mscoutermarsh/4142049 to your computer and use it in GitHub Desktop.
Grape API authentication methods
This file contains hidden or 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
# /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