I finally got "sign in with Facebook" to work with devise/omniauth, but when a user is signed in, the "sign in with facebook" link doesn't change to "sign out" and/or there is no visible sign out option.
here is my route.rb file
devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks", :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :sign_out => 'logout' } do
get 'login' =>'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
get 'signup' => 'registrations#new', :as => :new_user_registration
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
and here is the user file
class User < ActiveRecord::Base
devise :omniauthable, :omniauth_providers => [:facebook]
def self.find_for_facebook_oauth(auth, signed_in_resource=ni)
user = User.where (:provider => auth.provider, :uid => auth.uid).first
unless user
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.where
user = User.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20]
)
end
user
end
end
session controller:
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_path
end
def destroy
session.delete[:user_id] = nil
redirect_to root_path
end
end
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.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
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
end
and lastly, the application layout
<% if current_user %>
Signed in as <strong><%= current_user.name %></strong>!
<%= link_to "Sign out", destroy_user_session, id: "sign_out" %>
<% else %>
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
<% end %>
Not quite sure waht i keep doing wrong or why I'm having a hard time looking for the answer, so I thought it might be easier to just put up the code. I"m new at this, so any help is appreciated. Thanks!
If "Sign in with Facebook" is still showing, and Sign Out is not showing, i'm assuming the "Signed in as" Is also not showing.
This is most likely due to this current_user not being set, so this line is failing
<% if current_user %>
and so this code gets triggered
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
So I am assuming current_user is not actually getting set, therefore you're not really logged in. You can add the devise controller filter authenticate_user! to see if you're logged in or not. Something like
class ApplicationController
before_filter :authenticate_user!
end
Which will redirect you if you are not logged in.
Pry is a great tool for debugging these kinds of things
To debug with pry, you would add to your Gemfile
group :development do
gem "pry", "~> 0.9.12.4"
end
and run bundle install
you can then add this to your code
<%= binding.pry %>
<% if current_user %>
Signed in as <strong><%= current_user.name %></strong>!
<%= link_to "Sign out", destroy_user_session, id: "sign_out" %>
<% else %>
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
<% end %>
Go through your normal Login with Facebook workflow, and the Rails Server in your terminal will 'stop' at the `<% binding.pry %> and allow you to run commands, including checking variables.
In the terminal your rails console is running in, you should see something like
current_user ? "There is a current user set" : "No current user is set"
You may also just run
current_user
but the former has more verbose output. Some other useful debugging commands include
help
whereami
exit
!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With