Unauthorized error

108 views
Skip to first unread message

Marco Noronha

unread,
Jan 31, 2017, 5:47:00 AM1/31/17
to Devise

Using Rails 5 and this dependencies:


gem 'active_model_serializers', '~> 0.10.0'

gem 'devise', github: 'plataformatec/devise', branch: 'master'
gem 'devise_token_auth', github: 'lynndylanhurley/devise_token_auth', branch: 'master'


I created a controller:

module API::V1
  class CommunityPeopleController < AuthenticatedAPIController


With two methods:

# POST /community-people
def create

# DELETE /community-people/1
def delete


And configured it on the Routes:

namespace :api, defaults: { format: 'json' } do
    namespace :v1 do
      resources :communities

      post 'community-people',
          to: 'community_people#create'

      delete 'community-people/:id_community',
          to: 'community_people#delete'

    end
  end


But when I do the POST I get a 401 error status. This shows in Rails console:

Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 5ms (Views: 4.0ms | ActiveRecord: 0.0ms)


But the DELETE works fine. PS: I already check all the data and params I send to the server. It's completly fine. The user already is authenticated.

How can I fix this?


PS: MORE INFO: Check the AuthenticatedPIController

module API::V1
  class AuthenticatedAPIController < APIController
    before_action :authenticate_user!
  end
end


And APIController just extends ApplicationController:

  class APIController < ::ApplicationController


And last:

class ApplicationController < ActionController::API


Thanks


EDIT I've done some more tests... If I create a GET there's no problem with auth. The problem is with the POST method.

  // Routes
  get 'community-people/test',
      to: 'community_people#test'

// Controller
def test
  puts 'got here!'
end


And it works. I tried adding another stub POST action and still didnt work :(

Reply all
Reply to author
Forward
0 new messages