Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with authentication #20

Closed
quellhorst opened this issue Aug 27, 2014 · 15 comments
Closed

Error with authentication #20

quellhorst opened this issue Aug 27, 2014 · 15 comments

Comments

@quellhorst
Copy link

On our app we are using your devise_token_auth but getting an exception:

[PROJECT_ROOT]/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/dynamic_matchers.rb:26:in method_missing' NoMethodError: undefined methodfind_by_uid' for #Class:0x007f6622678fd8

Any idea what is the cause?

@lynndylanhurley
Copy link
Owner

  • Which route are you trying to access?
  • Have you included the model concern in your user model?
  • Have you run the migration?

@quellhorst
Copy link
Author

route is test#members_only mounted with
get 'members_only' => 'test#members_only'

in routes.rb I have:
mount_devise_token_auth_for 'User', at: '/auth'

in user.rb I have:
class User < ActiveRecord::Base
include DeviseTokenAuth::Concerns::User

I just connected to the server and we already have UID in the users table from the migration

@lynndylanhurley
Copy link
Owner

The error method_missing' NoMethodError: undefined method find_by_uid suggests that either uid isn't defined on the User model, or that the server is trying to run find_by_uid on the wrong model or class.

If you open a rails console using rails c, can you manually run find_by_uid for an existing user? For example, if a user's uid is 123, try running User.find_by_uid(123).

@quellhorst
Copy link
Author

It appears that the error was somehow caused by having:
gem 'activeadmin', github: 'gregbell/active_admin'

Active_admin was also using devise and I think that it somehow overwrote find_by_uid?

@lynndylanhurley
Copy link
Owner

That's really strange. I'll install active_admin and see if I can reproduce the bug.

@lynndylanhurley
Copy link
Owner

@quellhorst - I've installed active admin, but I still can't reproduce this bug. Are there any other gems that could be causing conflicts?

@quellhorst
Copy link
Author

@lynndylanhurley I'll look and see.

@pedroviana
Copy link

I was facing this issue and fix by changing regenerating different user model name like AdminUser for example, and everything works fine.

@godinezb
Copy link

godinezb commented Oct 24, 2016

I'm recently run into the same error. With devise_token_auth (0.1.39), when trying to access a specific resource:

undefined method `find_by_uid' for #Class:0x007f90096c6a60

I checked my migrations, and I'm able to run AppUser.find_by_uid("[email protected]"), the problem shows up when I try to access index action on my vehicle resource:

get '/app_users/:app_user_id/vehicles' => 'vehicles#index'

This is my index action:

module Api
  module V1
    class VehiclesController < ApiController
      before_action :authenticate_app_user!
      before_action :set_vehicle, only: [:show, :edit, :update, :destroy]

      def index
        render json: Vehicle.where(owner_id: params[:app_user_id])
      end
     [...]
  end
end

Any idea what might be causing Devise Token Auth to try to run 'find_by_uid' on my vehicles controller?

@pedroviana 's solution didn't worked for me because my user model is called AppUser and not User...

Thanks.

@pedroviana
Copy link

@godinezb are you using active_admin? if so try to define the devise_token_auth routes before active_admin routes like

`mount_devise_token_auth_for 'User', at: 'auth', controllers: {
registrations: 'auth/registrations',
passwords: 'auth/passwords'
}

devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)`

@godinezb
Copy link

Hey @pedroviana thanks for replying. But I'm not using Active admin, what I'm using is Active Model Serializers, I have checked in other issues and the versions I have shouldnt be causing errors, I'm feeling really desperate at this point..

@pedroviana
Copy link

@godinezb I am using version 0.1.36, can you try it?

@godinezb
Copy link

Using this gems:

gem 'active_model_serializers', '0.10.0.rc4'
gem 'devise_token_auth', '0.1.36'
gem 'devise', '3.5.2'

And I'm still getting the same errors:

Completed 500 Internal Server Error in 93ms (ActiveRecord: 4.0ms)
Reporting exception: undefined method `find_by_uid' for #<Class:0x007fdcaf932a68>

When my controller tries to get the json response, the method 'find_by_uid' its called in a non devise_token_auth model...

@pedroviana
Copy link

@godinezb type here the full implementation of devise_token_auth, migrations, routes, model and config file

@godinezb
Copy link

Sure, here are my configuration files.
Routes:

devise_for :app_users

namespace :api do
  scope :v1 do
    mount_devise_token_auth_for 'AppUser', at: 'auth'
  end
end

Model:

class AppUser < ActiveRecord::Base
  devise  :database_authenticatable, :recoverable, :rememberable,
          :trackable, :validatable, :registerable
  include DeviseTokenAuth::Concerns::User
  has_many :vehicle_owners, :class_name => 'Vehicle', :foreign_key => 'owner_id'
  has_many :vehicle_drivers, :class_name => 'Vehicle', :foreign_key => 'driver_id'
end

Config file:

DeviseTokenAuth.setup do |config|
  config.change_headers_on_each_request = false
end

Schema:

create_table "app_users", force: :cascade do |t|
    t.string   "name"
    t.string   "lastnames"
    t.boolean  "admin"
    t.integer  "family_id"
    t.datetime "created_at",                               null: false
    t.datetime "updated_at",                               null: false
    t.string   "email",                  default: "",      null: false
    t.string   "encrypted_password",     default: "",      null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,       null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.string   "provider",               default: "email", null: false
    t.string   "uid",                    default: "",      null: false
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.json     "tokens"
  end

  add_index "app_users", ["reset_password_token"], name:
    "index_app_users_on_reset_password_token", unique: true, using: :btree
  add_index "app_users", ["uid", "provider"], name: 
    "index_app_users_on_uid_and_provider", using: :btree

Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants