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.