I am using rails-api and need to use authenticate_with_http_token to process a header authorization token, but i get the following error:
NoMethodError (undefined method `authenticate_with_http_token' for ...)
Add the following to ApplicationController
include ActionController::HttpAuthentication::Token::ControllerMethods
Related
I am building a rails API and I'm using pundit for authorizations to the API. I am trying to define current_user for pundit with this method:
def pundit_user
User.find_by_other_means
end
I tried implementing it as a private method in my API base controller but it gives me this error:
"exception": "#<NoMethodError: undefined method `find_by_other_means' for #<Class:0x00007f9d25463768>\nDid you mean? find_or_create_by>"
Then I tried implementing it on my application controller and it gives me the following error:
"exception": "#<NameError: undefined local variable or method `current_user' for #<Api::V1::NewsController:0x00007f9d2516b038>>"
How can I define current_user in pundit?
You can define your own current_user in ApplicationController, or use pundit_user - as mentioned in the documentation
I'm building a ruby API using rails admin (which is working) and rails admin import which has this problem:
Can't verify CSRF token authenticity.
Tried to deactivate it with
module Api::V1
class ApiController < ApplicationController
skip_before_action :verify_authenticity_token
...
class ApplicationController < ActionController::API
skip_before_action :verify_authenticity_token
But the result was the same error. Any ideas?
Thank you
P.S:
Also getting this when I put the code above(as it apears not to be implemented)
"status":500,"error":"Internal Server Error"
"exception":"# ArgumentError: Before process_action callback :verify_authenticity_token has not been defined"
How did you setup rails_admin?
Did you try using different controllers for both API and RailsAdmin, adding the needed includes in the later? Namely including ActionController::RequestForgeryProtection as described in Using rails_admin with rails_api?
In a rails application, the first line of a controller class is:
before_filter :authenticate_user!
I need to put a breakpoint inside the method autheticate_user!.
I discovered that this is a method that is added to the class. So it's not inherited from the super class, If it were i could do:
def authenticate_user!
super.authenticate_user!
end
Since there's no authenticate_user! in the superclass, this gives me:
NoMethodError (undefined method `authenticate_user!' for #<User:0xbec65c0>)
My problem is that i don't know where to stop the execution during authentication, or how to implement a trick in order to intercept such process.
alias_method should work:
alias_method :orig_authenticate_user!, :authenticate_user!
def authenticate_user!
orig_authenticate_user!
end
For devise there is a very easy to use Before_filter to use in the controllers.
For some reason I can't get this to work for the Omniauth_facebook Gem. I followed the Railscast on Facebook Omniauth and also
before_filter :authenticate
def authenticate
redirect_to :login unless User.find_by_provider_and_uid(auth["provider"], auth["uid"])
end
end
but I get an error:
NameError in PostsController#new
undefined local variable or method `auth' for #<PostsController:0x007f9fbfa7ee58>
Any thoughts?
It can't find the variable named auth. So you need to check that auth variable initialized somewhere or not. As per my view, OmniAuth Facebook gem is storing the authenticated data in request env. Please refer this: https://github.com/mkdynamic/omniauth-facebook#auth-hash.
before_filter will execute before serving the request. so may be that causes the issues.
Hope that helps!!!
I think the auth variable you're after is request.env["omniauth.auth"]
Im trying to refresh the Geokit location to session after the user sign in. I have the following code.
# app/controllers/application_controller.rb
def after_sign_in_path_for(resource_or_scope)
session[:geo_location] = User.geocode(current_user.city)
end
But I'm getting the following error.
NoMethodError in Devise::SessionsController#create
undefined method `model_name' for Geokit::GeoLoc:Class
Seems like Geokit is not loading before the devise controller. Any idea?
The problem is after_sign_in_path_for must return a valid an url object and you are not doing it check its documentation.