gonzalo-bulnes / simple_token_authentication

Simple (and safe*) token authentication for Rails apps or API with Devise.
GNU General Public License v3.0
1.51k stars 238 forks source link

406 Errors on Authentication Test #364

Closed TensAndTwenties closed 4 years ago

TensAndTwenties commented 4 years ago

I have a rails app using devise, and Im trying to create a login via API for a mobile app built with React Native. Right now I'm just trying to get the ruby side of the equation working, to make an API that can authenticate a user. However, when I send my requests like so: https://site.com/api?user_email=(email)&user_token=(token), I get a 406 error.

To be honest Im not clear on the response I should be getting to verify that this is working. Im also confused how routing works with this tool, as the above link directs to the index action of the controller, and no routing info was specified in the installation instructions.

So far I have installed the simple_token_authentication. Ive altered my User model and API controller like so:

class User < ApplicationRecord
  acts_as_token_authenticatable

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

And heres my controller:

class Api::ThingsController < ApplicationController
    acts_as_token_authentication_handler_for User

    def index
    end

    def apiTest
        orders = Order.where(processed: false).order(created_at: :desc)
        render json: {status: 'SUCCESS',message: 'Contacted API Test', data: orders}, status: :ok
    end
end

And heres my config/routes.rb file:

Rails.application.routes.draw do
  devise_for :users
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'index#index'

  #API
    namespace :api do
      get '/', to: 'things#index'
      get '/apiTest', to: 'things#apiTest'
  end
end

Any help you can give me towards authenticating my users via API would be a huge help. Thanks very much for taking the time.