简体   繁体   中英

Devise reconfirmation hook

This question is very similar to Rails Devise: after_confirmation except I'm looking for more specifically a reconfirmation hook

My use case : I need to synchronise a new user email on some third party service (Intercom to be precise).

I have a first implementation using an API where I have put the logic I need there (all tidied up in a service)

However, I often end up doing a lot of maintenance using the console, and the most obvious things that come to mind is to perform

user.email = 'newemail@example.com'
user.confirm # or skip_reconfirmation
user.save

Using this, I do not fire my resynchronisation logic automatically. Is there a way to force some reconfirmation callback ? overriding after_confirmation does not seem to work

Not really an answer, but in the meantime I have monkeypatched my user class with the following (where xxx represents some custome methods added on the user class to sync to third party services the new email)

class User
  include User::Console if defined?('Rails::Console')
end

module User::Console
  extend ActiveSupport::Concern

  included do
    ## Override Devise confirmable to add warnings

    def confirmation_warning
      puts "WARNING !!".red
      puts "calling `#confirm` or `#skip_reconfirmation!` on the user does not sync to xxx !\n"\
        'Please either use [Name of custom confirmation service], '\
        'or use `user.sync_to_xxx` to propagate changes'.red
    end

    def confirm
      confirmation_warning
      super
    end

    def skip_reconfirmation!
      confirmation_warning
      super
    end
  end
end

I have the exact same use case, where I need to sync a new user email on a third party service.

The way I solved it is a before_update filter on the User model, and using email_changed? :

class User < ApplicationRecord    

  before_update :update_email_in_third_party_service

  private

  def update_email_in_third_party_service
    return unless self.valid? && self.email_changed?

    # We passed our check, update email in the third party service (preferably in a background job)
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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