public
Last active

  • Download Gist
application.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10
<html>
<head>
<%= javascript_include_tag :defaults %>
<%= fb_connect_javascript_tag %>
</head>
<body>
<%= fb_connect_javascript_tag %>
<%= init_fb_connect "XFBML" %>
</body>
</html>
gistfile2.rb
Ruby
1 2 3
def ensure_authenticated_to_facebook
set_facebook_session || create_new_facebook_session_and_redirect!
end
gistfile3.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
def set_facebook_session
# first, see if we already have a session
session_set = session_already_secured?
# if not, see if we can load it from the environment
unless session_set
session_set = create_facebook_session
session[:facebook_session] = @facebook_session if session_set
end
if session_set
capture_facebook_friends_if_available!
Session.current = facebook_session
end
return session_set
end
gistfile4.rb
Ruby
1 2 3
def create_facebook_session
secure_with_facebook_params! || secure_with_cookies! || secure_with_token!
end
gistfile5.txt
1
<%= authlogic_facebook_login_button :length => "long" %>
user_sessions_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
class UserSessionsController < ApplicationController
before_filter :check_fb, :only => :create
#...
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save && !@user_session.user.suspended?
unless @user_session.user.facebook_uid.nil?
@user_session.user.active = true
@user_session.user.save
end
 
flash[:notice] = t(:msg_welcome)
if @user_session.user.login_count > 1 && @user_session.user.last_login_at?
flash[:notice] << " " << t(:msg_last_login, @user_session.user.last_login_at.to_s(:mmddhhss))
end
redirect_back_or_default root_url
else
#...
end
end
#...
private
 
def check_fb
if params[:user_session].nil?
ensure_authenticated_to_facebook
end
end
end
user_sessions_controller_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
describe UserSessionsController do
describe "create" do
describe "facebook user" do
it "creates account based on facebook details" do
FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:post, "http://api.facebook.com/restserver.php", :body => Rails.root + "spec/fixtures/get_users_info.xml")
setup_fb_connect_cookies
 
post :create
 
response.should redirect_to(root_url)
@assigned_user = assigns[:user_session].user
@assigned_user.new_record?.should be_false
@assigned_user.active.should be_true
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.