Omar Omar - 1 month ago 9
Ruby Question

Facebook Profile Image & Data using Devise in Rails

I'm just starting to learn Rails so please forgive the dumb question. In my web app, I was able to set up devise successfully and then link it Facebook using Omni-auth gem. However, two things occur.

1) A person who signs in using Facebook doesn't register as logged in - the Navbar still shows "Login"

2) I added an avatar so users can show their profile picture. While a regular devise user can added it without a problem. Someone who logs in with Facebook doesn't have their image displayed.

Any help that you guys can give me would be so amazing, I feel like the issue might be in my user.rb file. I have listed below all my relevant code and my github url. Thank you again :)

Github-url: https://github.com/OmarZV/TY2

User.rb

class User < ApplicationRecord

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook]
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
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]
user.name = auth.info.name
user.image = auth.info.image
end
end
end


Application_controller.rb

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :avatar])
devise_parameter_sanitizer.permit(:account_update, keys: [:username, :avatar])
end
end


Application.html.erb

<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><%= link_to 'Posts', '#' %></li>
<% if current_user %>
<li><%= link_to 'Edit Profile',edit_user_registration_path %></li>
<li><%= link_to 'Logout', destroy_user_session_path, method: :delete %></li>

<li class="round-image-50"><%= image_tag(current_user.avatar.url(:thumb)) %></li>

<% else %>
<li><%= link_to 'Login', new_user_session_path %></li>
<% end %>
</ul>
</div>
</div><!-- /.navbar-collapse -->


Omniauth_Callbacks_Controller

class Users::OmniauthCallbacksController<Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model(e.g.app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if
is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end

Answer

Chris at Gorails.com helped me out on this one. The flaw was in two main areas, hope this helps someone.

1) User.rb file needs to be adjusted for the usage of paperclip gem

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]
  #user.name = auth.info.name
  user.avatar = URI.parse(auth.info.image)
end
  end

2)In my Environments/initialzers/devise.rb file as I'm using paperclip that needs to be adjusted as well.

config.omniauth :facebook, "App ID", "App Secret", secure_image_url: true, callback_url: "http://localhost:3000/users/auth/facebook/callback"