jerryvos-authlogic_facebook_connect 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +79 -0
- data/Rakefile +18 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/authlogic_facebook_connect.rb +10 -0
- data/lib/authlogic_facebook_connect/acts_as_authentic.rb +20 -0
- data/lib/authlogic_facebook_connect/helper.rb +29 -0
- data/lib/authlogic_facebook_connect/session.rb +136 -0
- data/lib/authlogic_facebook_connect/version.rb +0 -0
- metadata +63 -0
data/README.rdoc
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
== Install and use
|
2
|
+
|
3
|
+
=== 1. Install the facebooker gem and make Rails use it
|
4
|
+
|
5
|
+
$ sudo gem install facebooker
|
6
|
+
|
7
|
+
Make sure that you have setup you config/facebooker.yml to match your facebook application, make you application depended on the facebooker gem
|
8
|
+
|
9
|
+
config.gem "facebooker"
|
10
|
+
|
11
|
+
and run the xd_receiver generator to create the cross domain scripting bridge to make it possible for your application to communicate with facebook
|
12
|
+
|
13
|
+
$ script/generate xd_receiver
|
14
|
+
|
15
|
+
For more information on the facebooker gem checkout it's readme http://github.com/mmangino/facebooker/tree/master
|
16
|
+
|
17
|
+
=== 2. Install the Authlogic Facebook Connect plugin
|
18
|
+
|
19
|
+
$ script/plugin install git://github.com/kalasjocke/authlogic_facebook_connect.git
|
20
|
+
|
21
|
+
This plugin should soon be packed inside a gem.
|
22
|
+
|
23
|
+
=== 3. Make some changes to your database
|
24
|
+
|
25
|
+
class AddFacebookConnectFieldsToUser < ActiveRecord::Migration
|
26
|
+
def self.up
|
27
|
+
add_column :users, :name, :string
|
28
|
+
add_column :users, :facebook_uid, :integer, :limit => 8
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.down
|
32
|
+
remove_column :users, :facebook_uid
|
33
|
+
remove_column :users, :name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
=== 4. Make your layout look something like this
|
38
|
+
|
39
|
+
The important parts are the facebook html namespace, facebook javascript include and the facebook helper calls to init the facebooker gem.
|
40
|
+
|
41
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
42
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
43
|
+
|
44
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
|
45
|
+
<head>
|
46
|
+
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
47
|
+
<title>Your awsome facebook connected app</title>
|
48
|
+
|
49
|
+
<%= stylesheet_link_tag 'application' %>
|
50
|
+
<%= javascript_include_tag :defaults %>
|
51
|
+
|
52
|
+
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
|
53
|
+
</head>
|
54
|
+
<body>
|
55
|
+
|
56
|
+
<p style="color: green"><%= flash[:notice] %></p>
|
57
|
+
|
58
|
+
<%= fb_connect_javascript_tag %>
|
59
|
+
<%= init_fb_connect "XFBML" %>
|
60
|
+
|
61
|
+
<%= yield %>
|
62
|
+
|
63
|
+
</body>
|
64
|
+
</html>
|
65
|
+
|
66
|
+
=== 5. Add the Facebook Connect button to your login form
|
67
|
+
|
68
|
+
<%= authlogic_facebook_login_button %>
|
69
|
+
|
70
|
+
=== Notes
|
71
|
+
|
72
|
+
If you want to save some user data when connecting to facebook you can use the before_connect hook in your user model.
|
73
|
+
|
74
|
+
def before_connect(facebook_session)
|
75
|
+
self.name = facebook_session.user.name
|
76
|
+
end
|
77
|
+
|
78
|
+
For more information about what you can get form the facebook_session checkout the Facebooker gem rdoc.
|
79
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "jerryvos-authlogic_facebook_connect"
|
8
|
+
gem.summary = %Q{Extension of the Authlogic library to add Facebook Connect support built upon the excellent facebooker gem}
|
9
|
+
# gem.description = %Q{}
|
10
|
+
# gem.email = ""
|
11
|
+
gem.homepage = "http://github.com/kalasjocke/authlogic_facebook_connect"
|
12
|
+
gem.authors = ["kalasjocke"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'authlogic_facebook_connect'
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# require "authlogic_facebook_connect/version"
|
2
|
+
require "authlogic_facebook_connect/acts_as_authentic"
|
3
|
+
require "authlogic_facebook_connect/session"
|
4
|
+
require "authlogic_facebook_connect/helper"
|
5
|
+
|
6
|
+
if ActiveRecord::Base.respond_to?(:add_acts_as_authentic_module)
|
7
|
+
ActiveRecord::Base.send(:include, AuthlogicFacebookConnect::ActsAsAuthentic)
|
8
|
+
Authlogic::Session::Base.send(:include, AuthlogicFacebookConnect::Session)
|
9
|
+
ActionController::Base.helper AuthlogicFacebookConnect::Helper
|
10
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module AuthlogicFacebookConnect
|
2
|
+
module ActsAsAuthentic
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
extend Config
|
6
|
+
add_acts_as_authentic_module(Methods, :prepend)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Config
|
11
|
+
end
|
12
|
+
|
13
|
+
module Methods
|
14
|
+
def self.included(klass)
|
15
|
+
klass.class_eval do
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module AuthlogicFacebookConnect
|
2
|
+
module Helper
|
3
|
+
def authlogic_facebook_login_button(options = {})
|
4
|
+
# TODO: Make this with correct helpers istead of this uggly hack.
|
5
|
+
|
6
|
+
options[:controller] ||= "user_session"
|
7
|
+
options[:js] ||= :prototype
|
8
|
+
|
9
|
+
case options[:js]
|
10
|
+
when :prototype
|
11
|
+
js_selector = "$('connect_to_facebook_form')"
|
12
|
+
when :jquery
|
13
|
+
js_selector = "jQuery('#connect_to_facebook_form')"
|
14
|
+
end
|
15
|
+
|
16
|
+
output = "<form id='connect_to_facebook_form' method='post' action='/#{options[:controller]}'>\n"
|
17
|
+
output << "<input type='hidden' name='authenticity_token' value='#{form_authenticity_token}'/>\n"
|
18
|
+
output << "</form>\n"
|
19
|
+
output << "<script type='text/javascript' charset='utf-8'>\n"
|
20
|
+
output << " function connect_to_facebook() {\n"
|
21
|
+
output << " #{js_selector}.submit();\n"
|
22
|
+
output << " }\n"
|
23
|
+
output << "</script>\n"
|
24
|
+
options.delete(:controller)
|
25
|
+
output << fb_login_button("connect_to_facebook()", options)
|
26
|
+
output
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
module AuthlogicFacebookConnect
|
2
|
+
module Session
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
extend Config
|
6
|
+
include Methods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Config
|
11
|
+
# Should the user be saved with our without validations?
|
12
|
+
#
|
13
|
+
# The default behavior is to save the user without validations and then
|
14
|
+
# in an application specific interface ask for the additional user
|
15
|
+
# details to make the user valid as facebook just provides a facebook id.
|
16
|
+
#
|
17
|
+
# This is useful if you do want to turn on user validations, maybe if you
|
18
|
+
# just have facebook connect as an additional authentication solution and
|
19
|
+
# you already have valid users.
|
20
|
+
#
|
21
|
+
# * <tt>Default:</tt> true
|
22
|
+
# * <tt>Accepts:</tt> Boolean
|
23
|
+
def facebook_valid_user(value = nil)
|
24
|
+
rw_config(:facebook_valid_user, value, false)
|
25
|
+
end
|
26
|
+
alias_method :facebook_valid_user=, :facebook_valid_user
|
27
|
+
|
28
|
+
# What user field should be used for the facebook UID?
|
29
|
+
#
|
30
|
+
# This is useful if you want to use a single field for multiple types of
|
31
|
+
# alternate user IDs, e.g. one that handles both OpenID identifiers and
|
32
|
+
# facebook ids.
|
33
|
+
#
|
34
|
+
# * <tt>Default:</tt> :facebook_uid
|
35
|
+
# * <tt>Accepts:</tt> Symbol
|
36
|
+
def facebook_uid_field(value = nil)
|
37
|
+
rw_config(:facebook_uid_field, value, :facebook_uid)
|
38
|
+
end
|
39
|
+
alias_method :facebook_uid_field=, :facebook_uid_field
|
40
|
+
|
41
|
+
# Class representing facebook users we want to authenticate against
|
42
|
+
#
|
43
|
+
# * <tt>Default:</tt> klass
|
44
|
+
# * <tt>Accepts:</tt> Class
|
45
|
+
def facebook_user_class(value = nil)
|
46
|
+
rw_config(:facebook_user_class, value, klass)
|
47
|
+
end
|
48
|
+
alias_method :facebook_user_class=, :facebook_user_class
|
49
|
+
|
50
|
+
# Should a new user creation be skipped if there is no user with given facebook uid?
|
51
|
+
#
|
52
|
+
# The default behavior is not to skip (hence create new user). You may want to turn it on
|
53
|
+
# if you want to try with different model.
|
54
|
+
#
|
55
|
+
# * <tt>Default:</tt> false
|
56
|
+
# * <tt>Accepts:</tt> Boolean
|
57
|
+
def facebook_skip_new_user_creation(value = nil)
|
58
|
+
rw_config(:facebook_skip_new_user_creation, value, false)
|
59
|
+
end
|
60
|
+
alias_method :facebook_skip_new_user_creation=, :facebook_skip_new_user_creation
|
61
|
+
end
|
62
|
+
|
63
|
+
module Methods
|
64
|
+
def self.included(klass)
|
65
|
+
klass.class_eval do
|
66
|
+
validate :validate_by_facebook_connect, :if => :authenticating_with_facebook_connect?
|
67
|
+
end
|
68
|
+
|
69
|
+
def credentials=(value)
|
70
|
+
# TODO: Is there a nicer way to tell Authlogic that we don't have any credentials than this?
|
71
|
+
values = [:facebook_connect]
|
72
|
+
super
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def validate_by_facebook_connect
|
77
|
+
facebook_session = controller.facebook_session
|
78
|
+
self.attempted_record = facebook_user_class.find(:first, :conditions => { facebook_uid_field => facebook_session.user.uid }).try(:"#{klass}".to_s.underscore)
|
79
|
+
|
80
|
+
unless self.attempted_record || facebook_skip_new_user_creation
|
81
|
+
begin
|
82
|
+
# Get the user from facebook and create a local user.
|
83
|
+
#
|
84
|
+
# We assign it after the call to new in case the attribute is protected.
|
85
|
+
new_user = klass.new
|
86
|
+
if klass == facebook_user_class
|
87
|
+
new_user.send(:"#{facebook_uid_field}=", facebook_session.user.uid)
|
88
|
+
else
|
89
|
+
new_user.send(:"build_#{facebook_user_class.to_s.underscore}", :"#{facebook_uid_field}" => facebook_session.user.uid)
|
90
|
+
end
|
91
|
+
|
92
|
+
new_user.before_connect(facebook_session) if new_user.respond_to?(:before_connect)
|
93
|
+
|
94
|
+
self.attempted_record = new_user
|
95
|
+
|
96
|
+
if facebook_valid_user
|
97
|
+
errors.add_to_base(
|
98
|
+
I18n.t('error_messages.facebook_user_creation_failed',
|
99
|
+
:default => 'There was a problem creating a new user ' +
|
100
|
+
'for your Facebook account')) unless self.attempted_record.valid?
|
101
|
+
|
102
|
+
self.attempted_record = nil
|
103
|
+
else
|
104
|
+
self.attempted_record.save_with_validation(false)
|
105
|
+
end
|
106
|
+
rescue Facebooker::Session::SessionExpired
|
107
|
+
errors.add_to_base(I18n.t('error_messages.facebooker_session_expired',
|
108
|
+
:default => "Your Facebook Connect session has expired, please reconnect."))
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def authenticating_with_facebook_connect?
|
114
|
+
controller.set_facebook_session
|
115
|
+
attempted_record.nil? && errors.empty? && controller.facebook_session
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
def facebook_valid_user
|
120
|
+
self.class.facebook_valid_user
|
121
|
+
end
|
122
|
+
|
123
|
+
def facebook_uid_field
|
124
|
+
self.class.facebook_uid_field
|
125
|
+
end
|
126
|
+
|
127
|
+
def facebook_user_class
|
128
|
+
self.class.facebook_user_class
|
129
|
+
end
|
130
|
+
|
131
|
+
def facebook_skip_new_user_creation
|
132
|
+
self.class.facebook_skip_new_user_creation
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jerryvos-authlogic_facebook_connect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kalasjocke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-22 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- VERSION
|
28
|
+
- init.rb
|
29
|
+
- lib/authlogic_facebook_connect.rb
|
30
|
+
- lib/authlogic_facebook_connect/acts_as_authentic.rb
|
31
|
+
- lib/authlogic_facebook_connect/helper.rb
|
32
|
+
- lib/authlogic_facebook_connect/session.rb
|
33
|
+
- lib/authlogic_facebook_connect/version.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/kalasjocke/authlogic_facebook_connect
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Extension of the Authlogic library to add Facebook Connect support built upon the excellent facebooker gem
|
62
|
+
test_files: []
|
63
|
+
|