0

I am working in an rails application in which there are a option to signup user using his facebook account.

Now I am using OmniAuth gem to authenticate user from facebook. And saving/uploading user's profile photo using carrierwave.

But I can not upload and save users profile photo in my server, when I getting the user's profile photo link using OmniAuth.

I want not use remote link for profile photo, as any time it will be changed from facebook or my user will want to change his profile photo in my website.

Please help me that how I save in my server the user's profile photo from this facebook link?

Tauhidul Islam
  • 376
  • 2
  • 10
  • possible duplicate of [Rails: retrieving image from Facebook after Omniauth login with Devise](http://stackoverflow.com/questions/9848543/rails-retrieving-image-from-facebook-after-omniauth-login-with-devise). Check the comments on the accepted answer for more information. – Ashitaka Dec 07 '13 at 02:03

1 Answers1

0

I use carrierwave to accomplish that:

  1. Add this to your Gemfile, then '$ bundle install'

    #for image processing/uploading
    gem 'carrierwave'
    gem 'mini_magick'
    
  2. Generate a new uploader for the image files '$ rails g uploader Image', you can config this one as you like; I configured mine to store a thumbnail version in addition to the original, like so:

     #You need ImageMagick library to use this
     include CarrierWave::MiniMagick
    
     #This stores a thumbnail version and the original
     #You need ImageMagick
     version :thumb do
        process resize_to_fit: [50, 50]
     end
    
     #this validates file extensions
     def extension_whitelist
        %w(jpg jpeg gif png)
     end
    
  3. I used a separate model to store images in my db, but you can also store the image reference directly to the user model, either way, you just need to add a column with data type string to the model where you want to store the image reference. Something like '$ rails g migration AddImageToUserTable image:string' once you have it, add this to that model file:

      #this tells Carrierwave to use the image attribute in the
      #model to store the file reference
      mount_uploader :image, ImageUploader
    
  4. Finally (if you stored the reference in your user model) you can have something like this inside your user model file where you store the facebook responce:

    def self.create_from_provider_data(provider_data)    
      where(provider: provider_data.provider, uid: provider_data.uid).first_or_initialize do |user|  
        user.remote_picture_url = provider_data.info.image
        user.username = provider_data.info.name
        user.email = provider_data.info.email
        user.password = Devise.friendly_token[0, 20]
        user.oauth_token = provider_data.credentials.token
        user.oauth_expires_at = provider_data.credentials.expires_at
        user.save!        
      end
    end
    

I was 6 years late xD, but hope this helps someone. :)