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

NoMethodError (undefined method `valid_token?' for nil:NilClass) #1035

Closed
blaslaunchlabs opened this issue Nov 28, 2017 · 5 comments
Closed

Comments

@blaslaunchlabs
Copy link

blaslaunchlabs commented Nov 28, 2017

Hi I'm trying to use @resource.valid_token?(token, client_id) but i get a NoMethodError (undefined method valid_token? for nil:NilClass) below is my code. Am I doing wrong or this valid token is not working?

version : devise_token_auth 0.1.43.beta1 and 0.1.42
rails : 5.1.4
gems:
activeadmin 2.0.0.alpha


routes: 

  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  mount_devise_token_auth_for 'User', at: 'auth'

controller
class ApiController < ActionController::Base
  include DeviseTokenAuth::Concerns::SetUserByToken
end

class ApplicationController < ActionController::Base
end

module V1
  class mycontrollerController < ActionController::Base
   def create
     User.is_valid_token(token, client_id)
   end
  end
end


model 
class User < ActiveRecord::Base
  # Include default devise modules.
  devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable
          # :confirmable, :omniauthable
  include DeviseTokenAuth::Concerns::User
  def self.is_valid_token(token,client_id)
  	@resource.valid_token?(token, client_id)
  end
end
@zachfeldman
Copy link
Contributor

Hi @blaslaunchlabs please elaborate on the issue you're facing exactly?

@blaslaunchlabs
Copy link
Author

Hi, I'm trying to use @resource.valid_token?(token, client_id) but i get a NoMethodError (undefined method valid_token? for nil:NilClass). am I doing wrong or this valid token is not working?

@dks17
Copy link
Contributor

dks17 commented Nov 29, 2017

@blaslaunchlabs I think you need in your controller find a user firstly. As I realized valid_token? is object method and @ resource object is nil. Try something like this in your controller:

 def create
  @user = User.find(client_id)
  @user.valid_token?(token, client_id)
 end

@blaslaunchlabs
Copy link
Author

Thanks @dks17 its working that way.

@zachfeldman
Copy link
Contributor

Yay! Thanks @dks17 for the assist

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

3 participants