ankane / authtrail

Track Devise login activity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Track info from non devise model

trejo08 opened this issue · comments

I have a use case, I'm trying to save info from a non devise model that belongs_to a devise model, EG: User -> LoginDevice -> LoginActivity, and I would like to track when a LoginDevice is unknown, this is checked from a fingerprint field inside of the LoginDevice model, in fact, I have the follow logic inside of de create action in the Devise SessionsController

# user.rb
class User < ActiveRecord::Base
  has_many :login_devices
end
# login_device.rb
class LoginDevice < ActiveRecord::Base
  belongs_to :user
  has_many :login_activities, as: :user
end
# login_activity.rb
class LoginActivity < ActiveRecord::Base
  belongs_to :user, polymorphic: true, class_name: 'LoginDevice'
end
# sessions_controller.rb
def create
  super do
    dev = resource.login_devices.where(fingerprint: params[:user][:fingerprint]).first_or_create do |device|
        # some code here
    end
     # AuthTrail maybe could track from here but still not tracking from the LoginDevice model
  end
end

I know that I can just do something like LoginActivity.create!(data) but I would like to know if there is a way to do automatically in order to preserve the current logic but with a non devise model

Hey @trejo08, you can call AuthTrail.track manually, but it’s meant to be internal so could change in a future release.

def self.track(strategy:, scope:, identity:, success:, request:, user: nil, failure_reason: nil)

Hi @ankane, I tried to do that but not works for me, I called directly from the Rails Console and also inside of a controller method but still not working for me, always returns me errors.

can you provide me an example how to do that?, because when I try to call and pass arguments gives me these errors

When I try to call manually this is the error:

This is if I call without parameters:

AuthTrail.track

ArgumentError - missing keywords: strategy, scope, identity, success, request:
  app/controllers/users/sessions_controller.rb:14:in `block in create'
  app/controllers/users/sessions_controller.rb:7:in `create'
  lib/log_before_timeout.rb:9:in `call'

This is if I pass the arguments

AuthTrail.track("database_authenticatable", "user", "user@example.com", true, request, resource, nil)

ArgumentError - wrong number of arguments (given 7, expected 0):
  app/controllers/users/sessions_controller.rb:14:in `block in create'
  app/controllers/users/sessions_controller.rb:7:in `create'
  lib/log_before_timeout.rb:9:in `call'

Please correct me if I'm doing it in a bad way.

Thanks @ankane, now works fine