devise Devise & OmniAuth Multiple Providers Add Model Concern omniauth_attributes_concern.rb

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

module OmniauthAttributesConcern
    extend ActiveSupport::Concern
    module ClassMethods
        Add Methods here 
    end
end

In this concern we can create methods for each social media to fetch and store attributes.

def twitter params
  (params['info']['email'] = "dummy#{SecureRandom.hex(10)}@dummy.com") if params['info']['email'].blank?
  attributes = {
                 email: params['info']['email'],
                 first_name: params['info']['name'].split(' ').first,
                 last_name: params['info']['name'].split(' ').last,
                 username: params['info']['nickname'],
                 password: Devise.friendly_token
               }
  create(attributes)
end

Note: Twitter only returns an email address if the user has confirmed his/her email at Twitter; otherwise the nil value is returned.

  • We can add other social media accounts the same way we have added Twitter above.

  • The profile image from the social media account can also be fetched, and will be passed as

    remote_image_url: params['info']['image']
    

    Note: the above example is meant for the CarrierWave gem, and 'image' in remote_image_url is the DB column. You can use any other gem and pass params['info']['image'] to it.



Got any devise Question?