0

Switching from my own auth solution to Devise, I'm trying to integrate with my mail service, Convertkit. I'm trying to make devise call my method after confirming a user but I'm not sure how to call the resource.

I tried both these solutions: https://stackoverflow.com/a/25336930/6848493 https://stackoverflow.com/a/16423638/6848493

Here's my User model:

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :lastseenable, :timeoutable

  validates :terms_and_privacy_policy,
            acceptance: { accept: true }, on: :create, allow_nil: false
  validates :first_name, presence: true
  validates :last_name, presence: true

  def confirm!
    super
    subscribe self
    self.send_convertkit_failure unless @ck_response_code == 200
  end

# Send email to admin in case of ConvertKit API failure
  def send_convertkit_failure
    UserMailer.convertkit_failure(self).deliver_now
  end

Here's my user helper:

module UsersHelper
  def subscribe(user)
    response = HTTParty.post("https://api.convertkit.com/v3/forms/form/subscribe", 
      query: {email: user.email, first_name: user.name, course_opted: true, api_secret: 'my secret' })
    @ck_response_code = response.code
  end
end

There is no error message, it confirms the user okay but it doesn't subscribe them to convertkit. I've tried the same code but with resource as the object but that throws an error.

As for the second method (writing to after_confirmation), it errors saying the user has already been confirmed.

Would really appreciate your input, thanks in advance.

Maayan Naveh
  • 340
  • 3
  • 20
  • Maybe the Model can not access the helper? Try [this](https://stackoverflow.com/a/6887426/5239030) – iGian May 08 '18 at 10:43
  • Thank you so much, @iGian, I tried moving it to the User model to eliminate any chance of that being the problem, and the same happens - user gets confirmed, nothing gets to convertkit. Any ideas? – Maayan Naveh May 08 '18 at 10:52
  • Maybe you already did, but.. Place a debug method in `subscribe` (for example printing to console `@ck_response_code`), so you can see if the method is called. Try sending manually the query to convertkit, to test the code. – iGian May 08 '18 at 12:10
  • @iGian thank you so much for your help, I finally found the culprit :) will post my working code as the answer. – Maayan Naveh May 09 '18 at 11:20

1 Answers1

0

Finally got this to work, in my User model:

  # Override Devise::Confirmable#after_confirmation
  def after_confirmation
    subscribe self
    self.send_convertkit_failure unless @ck_response_code == 200
  end
Maayan Naveh
  • 340
  • 3
  • 20