Filter chain halted as :authenticate_with_token! rendered or redirected Rails API

4.2k Views Asked by At

So I am working on a location-based api with rails where a location has many users and a user belongs_to a location at a particular time. This means the user table will contain a location_id column which serves as the foreign key. This, however, is different from most of the apps I have worked on in which the user will have many 'products' for example. So, I have this in my locations_controller.rb file

class Api::V1::LocationsController < ApplicationController
    before_action :authenticate_with_token!, only: [:create]
    respond_to :json

    def show
        respond_with Location.find(params[:id])
    end

    def create
        # @location = current_user.locations.build(location_params)
        @location = Location.new(location_params)
        current_user.location = @location
        current_user.save
        if @location.save
            render json: @location, status: :created, location: [:api, :v1, @location]
        else
            render json: {errors: @location.errors}, status: :unprocessable_entity
        end
    end

    private


    def location_params
        params.require(:location).permit(:latitude, :longitude, :address)
    end
end

But I get the error

Started POST "/api/v1/locations" for 127.0.0.1 at 2016-12-20 10:05:50 +0100
Processing by Api::V1::LocationsController#create as JSON
  Parameters: {"location"=>{"latitude"=>"2.23413", "longitude"=>"2.908019", "address"=>"Sims1 streets"}}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."auth_token" IS NULL LIMIT 1
Filter chain halted as :authenticate_with_token! rendered or redirected
Completed 401 Unauthorized in 5ms (Views: 0.2ms | ActiveRecord: 0.5ms)

My Question is: How do I solve this? I have searched everywhere but all to no avail. Please help

My authenticable.rb file is

module Authenticable

  # Devise methods overrides. This finds the user by auth_token and it's sent to the server
  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
1

There are 1 best solutions below

7
On

You are calling a callback befote the create action

before_action :authenticate_with_token!, only: [:create]

so whenever you will go to this action it will ask for a valid authentication token,the action will not be performed until a valid authentication token is provided.

As you can see in your console

 User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."auth_token" IS NULL LIMIT 1

The user auth token is null.

Please try sending a valid authtoken to authenticate user before craete action.

You might be having a auth_token column in your users table. Try finding the authentication_token for any user and pass it in your url

i.e

 /api/v1/locations?user_token= your token