<commit_before># Instantiate Castle client on every request
Warden::Manager.on_request do |warden|
  warden.request.env['castle'] =
    Castle::Client.new(warden.request, warden.cookies)
end

# Track logout.succeeded
Warden::Manager.before_logout do |record, warden, opts|
  if record.respond_to?(:castle_id)
    unless record.respond_to?(:castle_do_not_track?) && record.castle_do_not_track?
      castle = warden.request.env['castle']
      begin
        castle.track(user_id: record._castle_id, name: '$logout.succeeded')
      rescue ::Castle::Error => e
        if Devise.castle_error_handler.is_a?(Proc)
          Devise.castle_error_handler.call(e)
        end
      end
    end
  end
end

# Track login.failed
Warden::Manager.before_failure do |env, opts|
  if opts[:action] == 'unauthenticated' && opts[:username]

    user_id = if opts[:user].respond_to?(:castle_id)
      opts[:user]._castle_id
    end

    castle = env['castle']
    begin
      castle.track(
        name: '$login.failed',
        user_id: user_id,
        details: {
          '$login' => opts[:username]
        })
    rescue ::Castle::Error => e
      if Devise.castle_error_handler.is_a?(Proc)
        Devise.castle_error_handler.call(e)
      end
    end
  end
end

# Track login.succeeded
Warden::Manager.after_set_user :except => :fetch do |record, warden, opts|
  if record.respond_to?(:castle_id)
    unless record.respond_to?(:castle_do_not_track?) && record.castle_do_not_track?
      castle = warden.request.env['castle']
      begin
        castle.identify(record._castle_id, {
          email: record.email
        })
        castle.track(user_id: record._castle_id, name: '$login.succeeded')
      rescue ::Castle::Error => e
        if Devise.castle_error_handler.is_a?(Proc)
          Devise.castle_error_handler.call(e)
        end
      end
    end
  end
end<commit_msg>Don't automatically identify user during login<commit_after># Instantiate Castle client on every request
Warden::Manager.on_request do |warden|
  warden.request.env['castle'] =
    Castle::Client.new(warden.request, warden.cookies)
end

# Track logout.succeeded
Warden::Manager.before_logout do |record, warden, opts|
  if record.respond_to?(:castle_id)
    unless record.respond_to?(:castle_do_not_track?) && record.castle_do_not_track?
      castle = warden.request.env['castle']
      begin
        castle.track(user_id: record._castle_id, name: '$logout.succeeded')
      rescue ::Castle::Error => e
        if Devise.castle_error_handler.is_a?(Proc)
          Devise.castle_error_handler.call(e)
        end
      end
    end
  end
end

# Track login.failed
Warden::Manager.before_failure do |env, opts|
  if opts[:action] == 'unauthenticated' && opts[:username]

    user_id = if opts[:user].respond_to?(:castle_id)
      opts[:user]._castle_id
    end

    castle = env['castle']
    begin
      castle.track(
        name: '$login.failed',
        user_id: user_id,
        details: {
          '$login' => opts[:username]
        })
    rescue ::Castle::Error => e
      if Devise.castle_error_handler.is_a?(Proc)
        Devise.castle_error_handler.call(e)
      end
    end
  end
end

# Track login.succeeded
Warden::Manager.after_set_user :except => :fetch do |record, warden, opts|
  if record.respond_to?(:castle_id)
    unless record.respond_to?(:castle_do_not_track?) && record.castle_do_not_track?
      castle = warden.request.env['castle']
      begin
        castle.track(user_id: record._castle_id, name: '$login.succeeded')
      rescue ::Castle::Error => e
        if Devise.castle_error_handler.is_a?(Proc)
          Devise.castle_error_handler.call(e)
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12048846675712348, 'fraction_numerical': 0.0, 'mean_word_length': 3.1230425055928412, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3713226', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1216, 'n_words': 231}