简体   繁体   English

确认后设计自定义动作

[英]Devise custom after confirmation action

Switching from my own auth solution to Devise, I'm trying to integrate with my mail service, Convertkit. 从我自己的身份验证解决方案切换到Devise,我试图与我的邮件服务Convertkit集成。 I'm trying to make devise call my method after confirming a user but I'm not sure how to call the resource. 我在确认用户后尝试使devise调用我的方法,但不确定如何调用资源。

I tried both these solutions: https://stackoverflow.com/a/25336930/6848493 https://stackoverflow.com/a/16423638/6848493 我尝试了这两种解决方案: 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. 没有错误消息,它确认用户还可以,但是没有订阅他们的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. 至于第二种方法(写入after_confirmation),错误地指出用户已经被确认。

Would really appreciate your input, thanks in advance. 在此先感谢您的投入。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM