Skip to content

Commit

Permalink
Track last sign in date (#553)
Browse files Browse the repository at this point in the history
* add trackable module to devise

* disable IP adress logging in trackable module of devise

* extend comment to explain better what happens

* remove redundant lines in comment
  • Loading branch information
fosterfarrell9 authored Nov 12, 2023
1 parent ca8e298 commit 76cb7c0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
17 changes: 16 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class User < ApplicationRecord
include ApplicationHelper

# use devise for authentification, include the following modules
devise :database_authenticatable, :registerable,
devise :database_authenticatable, :registerable, :trackable,
:recoverable, :rememberable, :validatable, :confirmable, :lockable

# a user has many subscribed lectures
Expand Down Expand Up @@ -751,6 +751,21 @@ def can_update_personell?(lecture)
return false
end

# see https://github.com/heartcombo/devise/issues/4849#issuecomment-534733131
# We use the Devise::Trackable module to track sign-in count and current/last
# sign-in timestamp. However, we don't want to track IP address, but Trackable
# tries to, so we have to manually override the accessor methods so they do
# nothing.

def current_sign_in_ip
end

def last_sign_in_ip=(_ip)
end

def current_sign_in_ip=(_ip)
end

private

def set_defaults
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddDeviseTrackableColumnsToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :sign_in_count, :integer, default: 0, null: false
add_column :users, :current_sign_in_at, :datetime
add_column :users, :last_sign_in_at, :datetime
add_column :users, :current_sign_in_ip, :string
add_column :users, :last_sign_in_ip, :string
end
end
7 changes: 6 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_04_27_124337) do
ActiveRecord::Schema[7.0].define(version: 2023_11_01_100015) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -848,6 +848,11 @@
t.datetime "locked_at", precision: nil
t.text "image_data"
t.string "ghost_hash"
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"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
Expand Down

0 comments on commit 76cb7c0

Please sign in to comment.