1

In my rails 5 application I have used "devise_token_auth" gem for developing token based API's for authentication purpose. Till date everything was working good, but suddenly I am getting below error,

NoMethodError (undefined method `create_token' for #<User:0x00000002268570>

Could not find any solution on google. Below is the full error,

Started POST "/api/v1/auth/sign_in" for ::1 at 2018-06-05 16:32:34 +0530
   (28.8ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
   (11.0ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
Processing by DeviseTokenAuth::SessionsController#create as */*
  Parameters: {"email"=>"admin@example.com", "password"=>"[FILTERED]", "session"=>{"email"=>"admin@example.com", "password"=>"[FILTERED]"}}
Unpermitted parameter: :session
Unpermitted parameter: :session
  User Load (12.3ms)  SELECT  `users`.* FROM `users` WHERE (BINARY email = 'admin@example.com' AND provider='email') ORDER BY `users`.`id` ASC LIMIT 1
Unpermitted parameter: :session
Unpermitted parameter: :session
Completed 500 Internal Server Error in 395ms (ActiveRecord: 92.9ms)

NoMethodError (undefined method `create_token' for #<User:0x00000002268570>
Did you mean?  created_at):
Hemant Patil
  • 331
  • 2
  • 18

1 Answers1

0

I solved this error by using the Excluding Modules section of the docs.

# app/models/user.rb
class User < ActiveRecord::Base

  # notice this comes BEFORE the include statement below
  # also notice that :confirmable is not included in this block
  devise :database_authenticatable, :recoverable,
         :trackable, :validatable, :registerable,
         :omniauthable

  # note that this include statement comes AFTER the devise block above
  include DeviseTokenAuth::Concerns::User
end

Including the DeviseTokenAuth::Concerns::User was what solved the undefined method problem, but I needed this section because it required confirmation (I put the include before the devise block)

Adam D
  • 414
  • 4
  • 9