1

I have a app running on devise and omniauth for user registrations. However when someone logins with facebook using omniauth I want to display some extra info for that user. However I can't find a way to identify if that user is signed in with facebook.

I believe it has something to do with the devise_helper (user_signed_in?) but I'm not sure how to use it with omniauth.

user.rb

  def self.find_for_facebook_oauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.name = auth.info.name
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.username = auth.info.email
      user.token = auth.credentials.token
      user.save!
    end
  end

devise.rb

config.omniauth :facebook, ENV["APP_ID"], ENV["APP_SECRET"],
      {:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}} 

code

<% if current_user.token %>
    <% @graph = Koala::Facebook::API.new(current_user.token) %>
    <%= @friends = @graph.get_connections("me", "friends").to_a %>
        <%= @friends.each do |friend| %>
            <%= puts friend["name"] %>
        <% end %>
<% end %>
Leo Costa
  • 371
  • 3
  • 8
  • 18
  • i don't understand your question. do you want to check if the user is signed in at all, signed in via facebook for a particular session, or in general or what? – phoet Apr 27 '14 at 18:35
  • Sorry for the misunderstanding. I want to display some information only to the users that are signed in with facebook. I'll edit the question with the code I'm trying to make it work. – Leo Costa Apr 27 '14 at 21:56
  • 1
    so like `if current_user.provider == 'facebook'`? – phoet Apr 28 '14 at 02:56
  • I tried using: `current_user.provider == 'facebook'` but I get the error `undefined method 'provider'` when trying to access the page without logging in with facebook. I want the page to be available both for devise users and facebook users, just that little information that I want to display to users logged in with facebook. – Leo Costa Apr 29 '14 at 01:54
  • 3
    I think what you want to do is something like `if user_signed_in? && current_user.provider=='facebook'` You want to test whether someone is actually signed in (and so current_user is set) before you try to access a property on current_user – Kevek Apr 29 '14 at 03:16
  • Best answer here: https://stackoverflow.com/questions/15955139/how-to-check-if-current-user-is-logged-in-through-omniauth – Gianpaolo Scrigna Nov 09 '21 at 14:45

0 Answers0