kurenn / market_place_api

The API on Rails tutorial application
142 stars 68 forks source link

Filter chain halted as :authenticate_with_token! rendered or redirected Completed 401 Unauthorized #80

Open Orinameh opened 7 years ago

Orinameh commented 7 years ago

When I set before_action :authenticate_with_token! in products controller and I try to create a product after I login from Postman, I get the error "Not authenticated" with the message in my server as "Filter chain halted as :authenticate_with_token! rendered or redirected Completed 401 Unauthorized". Any help on how to resolve this? Thank you in anticipation

kurenn commented 7 years ago

You need to send the Authorization header with a valid token, are you doing this?

Orinameh commented 7 years ago

How do I go about that? I am currently following your method. This is what I have got in the authenticate.rb file @kurenn. All tests are passing but when I make a post request associating a user with another model, in your case, product, from my android app, I get Unauthorized. Please help.

`module Authenticable

def current_user @current_user ||= User.find_by(auth_token: request.headers['Authorization']) end

def authenticate_with_token! render json: { errors: "Not authenticated" }, status: :unauthorized unless user_signed_in? end

def user_signed_in? current_user.present? end

end`

But I get the unauthorized error when I use before_action :authenticate_with_token! in any controller

kurenn commented 7 years ago

Which version of Rails are you using?