ruby-on-railsoauth-2.0devisedevise-confirmable

Generate Custom token for Confirmable devise


I am using devise:confirmable, and want to use custom token, as I need to append some data in the confirmation token itself. How can it be configured ?

User Model :

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :password_archivable

Solution

  • Devise uses before create callback to generate token.

    before_create :generate_confirmation_token, if: :confirmation_required?
    

    You can override the generate_confirmation_token method from devise in your User model.

    Bellow is default behavior of the method.

        # Generates a new random token for confirmation, and stores
        # the time this token is being generated in confirmation_sent_at
        def generate_confirmation_token
          if self.confirmation_token && !confirmation_period_expired?
            @raw_confirmation_token = self.confirmation_token
          else
            self.confirmation_token = @raw_confirmation_token = Devise.friendly_token
            self.confirmation_sent_at = Time.now.utc
          end
        end
    

    Check this module for more info.