0
votes

Ryan Bates gives great screencast http://railscasts.com/episodes/360-facebook-authentication how to use "omniauth-facebook" gem. But there is some issues with:

#application_controller.rb
private

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

It sets @current_user too late for standart Devise auth protection for actions in controllers:

before_filter :authenticate_user!, :except => [:index, :show]

So it redirects to sign in page, even @current_user is aviable in views...

Maybe anyone knows how to fix it?

PS I saw few tricks with redirects handler, but I think there should be better decision...

1

1 Answers

0
votes

I have found the simple way to sign in using standart Devise method. Based on this tutorial, just set code in sessions_controller:

#sessions_controller.rb
class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    sign_in user
    redirect_to root_url
  end
end

And you can delete this from application controller:

private

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user