Created
July 11, 2011 03:15
-
-
Save tomykaira/1075273 to your computer and use it in GitHub Desktop.
original: https://github.com/nov/fb_graph_sample/blob/master/lib/authentication.rb / current_fb instead of current_user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Authentication | |
class Unauthorized < StandardError; end | |
def self.included(base) | |
base.send(:include, Authentication::HelperMethods) | |
base.send(:include, Authentication::ControllerMethods) | |
ActiveSupport.on_load(:action_controller) do | |
helper_method :current_fb | |
end | |
end | |
module HelperMethods | |
def current_fb | |
@current_fb ||= Facebook.find(session[:current_fb]) | |
rescue ActiveRecord::RecordNotFound | |
nil | |
end | |
def authenticated? | |
!current_fb.blank? | |
end | |
end | |
module ControllerMethods | |
def require_authentication | |
authenticate Facebook.find_by_id(session[:current_fb]) | |
rescue Unauthorized => e | |
redirect_to root_url and return false | |
end | |
def authenticate(user) | |
raise Unauthorized unless user | |
session[:current_fb] = user.id | |
end | |
def unauthenticate | |
current_fb.destroy | |
@current_fb = session[:current_fb] = nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment