ruby-on-railsrubydeviseshrine

Getting FB profile photo with Shrine


I'm using Devise for authentication in my project and I'm having troubles getting the image when logging through FB with Shrine gem.

I have standard devise method to get data from Omniauth provider in user model:

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0, 20]
      # Standard version
      user.avatar = auth.info.image
      # For Google:
      # user.avatar_remote_url = auth.info.image
    end
  end

The commented line works for logging with Google and sets the image properly as auth.info.image returns link which, when pasted into browser, opens an image in the browser. For FB however, this line of code results in failing to persist the user - in case of FB auth.info.image returns a link which downloads the file directly to HDD if pasted into browser.

The standard version on the other hand results in error 809: unexpected token at 'http://graph.facebook.com/v4.0/xxxxxxxxxxx/picture?access_token=xxxxxxxx when trying to sign up.

In another project I'm successfully getting the image from fb the standard way but there I'm using paperclip. I definitely have a proper set up set for Shrine and user prepared for the data (double checked it, and also - it works for Google oauth with the _remote_url helper) - I'm not sure though how to properly process the data received from FB with Shrine. Any ideas?


Solution

  • Under https://github.com/simi/omniauth-facebook#configuring it states that default configuration for provider is secure_image_url: true which doesn't seem to be the case though. The answer was to add secure_image_url: true to my provider hash for Facebook, it then returns https link instead of http via auth.info.image which can be assigned to user.avatar_remote_url. Question can be closed.