I am using switch_user https://github.com/flyerhzm/switch_user rails
gem
to login as any other devise user from an admin account. When I use
switch_user for an account, the devise session updates all the devise
trackable attributes for that user such as:
t.integer “sign_in_count”,
t.datetime “current_sign_in_at”
t.datetime “last_sign_in_at”
t.string “current_sign_in_ip”
t.string “last_sign_in_ip”
which I don’t want to happen. These updates should happen only when an
end
user actually logs in and not for an admin account through switch_user.
How can I achieve this?
Thanks.
Regards,
Ankur
There is a way provided by devise gem to skip the Trackable fields
updates
for a specific request. Devise uses a condition as :
if record.respond_to?(:update_tracked_fields!) &&
warden.authenticated?(options[:scope]) &&
!warden.request.env[“devise.skip_trackable”]
Above condition can be found at
# frozen_string_literal: true
# After each sign in, update sign in time, sign in count and sign in IP.
# This is only triggered when the user is explicitly set (with set_user)
# and on authentication. Retrieving the user from session (:fetch) does
# not trigger it.
Warden::Manager.after_set_user except: :fetch do |record, warden, options|
if record.respond_to?(:update_tracked_fields!) && warden.authenticated?(options[:scope]) && !warden.request.env['devise.skip_trackable']
record.update_tracked_fields!(warden.request)
end
end
In whichever request you want to skip the Trackable fields updates you
just
need to set an variable devise.skip_trackable from environment as :
prepend_before_filter { env[“devise.skip_trackable”] = true }
This will skip the updating of following columns:
sign_in_count
current_sign_in_at
last_sign_in_at
current_sign_in_ip
last_sign_in_ip
Please refer
Add env["devise.skip_trackable"] for polling requests · Issue #953 · heartcombo/devise · GitHub more
details.