NoMethodError: undefined method `current_sign_in_at' for #User:0x000055ce01dcf0a8 by using Devise_token_auth rails gem is not working
Asked Answered
G

4

11

NoMethodError: undefined method `current_sign_in_at' for #User:0x000055ce01dcf0a8

I think it is a session method error of some sort

I have an angular6 app for frontend and rails for backend, so the best option for me was to opt for devise_token_auth and ng_token_auth for user authentication.

I installed devise_token_auth gem followed by executing this line of code in terminal

"rails generate devise_token_auth:install User auth"

and on migration there was an error, I solved the issue by adding

"extend Devise::Models"

to the USER model and then migration had worked, then I created a user in the backend and tried to call sign_in using postman and the error "NoMethodError: undefined method `current_sign_in_at' for #User:0x000055ce01dcf0a8" came

I want the user to get authenticated using this gem or some other gem if they exist

Goraud answered 17/4, 2019 at 21:0 Comment(2)
Check that your migration includes the proper column definition, and if not you may need to either rebuild your database, or add a new migration to add that column.Levitus
thank you, the columns for trackable was not generated in the migration, that was the reason why it was giving me the error, thanks for the helpGoraud
P
30

I had this issue recently and it turns out I didn't have the trackable fields in my migration. There are two ways to fix this:

Option one. Add a new migration that adds the trackable fields to User

## Trackable
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

Run rake db:migrate

The second option: run a down migration

Start with this command - add your migration version number

rake db:migrate:down VERSION=xxxxxxxxxxxxxx

You should then be able to add the trackable fields to the migration file and then run

rake db:migrate up VERSION=xxxxxxxxxxxxxx

Run rake db:migrate

Pronate answered 31/5, 2019 at 14:15 Comment(1)
If you are using a DB that doesn't support the inet type, you can use string.Cloninger
R
3

@olivrg Rails 7 doesn't support t.inet I must use t.string

## Trackable
t.integer  :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string   :current_sign_in_ip
t.string   :last_sign_in_ip
Roxyroy answered 17/11, 2022 at 15:31 Comment(0)
C
2

@olivrg's suggestion to run a migration to add the trackable fields to the User model worked for me.

My migration looked like this:

class AddTrackableFieldsToUser < ActiveRecord::Migration[6.0]
  def change
    change_table :users do |t|
      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
    end
  end
end
Codicodices answered 22/9, 2019 at 6:17 Comment(0)
M
1

remove trackable in your user.rb file, like this:

class User < ActiveRecord::Base
   # devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable

tested in rails 7.0.6, devise: 4.9.2

Millford answered 18/9, 2023 at 9:14 Comment(1)
This will remove the trackable functionality, but was the quickest solution in my case.Mean

© 2022 - 2024 — McMap. All rights reserved.