How to Use authenticate_or_request_with_http_token in grape ?

479 views
Skip to first unread message

afiq nymous

unread,
Oct 29, 2015, 11:45:36 PM10/29/15
to Grape Framework Discussion
Hey guys, how can I implement authenticate_or_request_with_http_token in grape API. Below is my code :

module Articles
class ArticleData < Grape::API
include ActionController::HttpAuthentication::Token::ControllerMethods
#   http_basic do |email, password|
# user = User.find_by_email(email)
# user && user.valid_password?(password)
# end

before do
error!("401 Unauthorized", 401) unless authenticated
end

helpers do
def authenticated
authenticate_or_request_with_http_token do |token, options|
apiKey = ApiKey.where(auth_token: token).first
    #ApiKey.exists?(access_token: token)
  end
end
end
resource :article_data do

desc "Return all article data"
get do
Article.all
end

desc "create a new article"
## This takes care of parameter validation
params do
requires :title, type: String
requires :author, type: String
#requires :content, type: Text
end

#This takes care of creating article
post do
Article.create!({
title:params[:title],
content:params[:content],
author:params[:author],
user_id:params[:user_id],
image_url:params[:image_url]
})
end

desc "update article"
# this takes care of parameter validation
params do
requires :image_url, type: String
end

put ':id' do
Article.find(params[:id]).update({
content:params[:content],
image_url:params[:image_url]
})
end

desc "delete article by id"
# this takes care of parameter validation
params do 
requires :id, type: String
end

delete ':id' do
Article.find(params[:id]).destroy!
end

end

end
end

I got this error NoMethodError (undefined method `authenticate_or_request_with_http_token' for #<Grape::Endpoint:0x007ff325974c10>): when I run the curl command : curl http://localhost:3000/api/v1/article_data.json. Any help will be much appreciated. Thank you in advance.

Daniel Doubrovkine

unread,
Oct 30, 2015, 2:57:42 PM10/30/15
to ruby-...@googlegroups.com
AFAIK authenticate_or_request_with_http_token is a Rails construct, not something Grape knows anything about.

You can do token based auth with many other gems, check out http://www.ruby-grape.org/projects/ under auth.

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages