Skip to content

Commit

Permalink
Add facebook login!
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyhaines committed Sep 4, 2011
1 parent fc295e8 commit e2ebe82
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 83 deletions.
8 changes: 5 additions & 3 deletions Gemfile
@@ -1,17 +1,19 @@
source 'http://rubygems.org'

gem 'rails', '3.1.0.rc6'
gem 'rails', '~>3.1'

gem 'haml'
gem 'devise'
gem 'gravtastic'

gem 'omniauth'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.1.0.rc"
gem 'coffee-rails', "~> 3.1.0.rc"
gem 'sass-rails'
gem 'coffee-rails'
gem 'uglifier'
end

Expand Down
159 changes: 107 additions & 52 deletions Gemfile.lock
@@ -1,125 +1,179 @@
GEM
remote: http://rubygems.org/
specs:
actionmailer (3.1.0.rc6)
actionpack (= 3.1.0.rc6)
actionmailer (3.1.0)
actionpack (= 3.1.0)
mail (~> 2.3.0)
actionpack (3.1.0.rc6)
activemodel (= 3.1.0.rc6)
activesupport (= 3.1.0.rc6)
actionpack (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.2)
rack-cache (~> 1.0.2)
rack-mount (~> 0.8.1)
rack-test (~> 0.6.0)
sprockets (~> 2.0.0.beta.12)
activemodel (3.1.0.rc6)
activesupport (= 3.1.0.rc6)
bcrypt-ruby (~> 2.1.4)
rack-cache (~> 1.0.3)
rack-mount (~> 0.8.2)
rack-test (~> 0.6.1)
sprockets (~> 2.0.0)
activemodel (3.1.0)
activesupport (= 3.1.0)
bcrypt-ruby (~> 3.0.0)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.0.rc6)
activemodel (= 3.1.0.rc6)
activesupport (= 3.1.0.rc6)
activerecord (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
arel (~> 2.2.1)
tzinfo (~> 0.3.29)
activeresource (3.1.0.rc6)
activemodel (= 3.1.0.rc6)
activesupport (= 3.1.0.rc6)
activesupport (3.1.0.rc6)
activeresource (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
activesupport (3.1.0)
multi_json (~> 1.0)
addressable (2.2.6)
addressable (2.2.4)
ansi (1.3.0)
arel (2.2.1)
bcrypt-ruby (2.1.4)
bcrypt-ruby (3.0.0)
builder (3.0.0)
coffee-rails (3.1.0.rc.6)
coffee-rails (3.1.0)
coffee-script (>= 2.2.0)
railties (~> 3.1.0.rc1)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.1.2)
devise (1.4.2)
bcrypt-ruby (~> 2.1.2)
devise (1.4.4)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
erubis (2.7.0)
execjs (1.2.4)
multi_json (~> 1.0)
faraday (0.6.1)
addressable (~> 2.2.4)
multipart-post (~> 1.1.0)
rack (< 2, >= 1.1.0)
gravtastic (3.2.6)
grb (0.4.1)
haml (3.1.2)
heroku (2.4.2)
heroku (2.6.1)
launchy (>= 0.3.2)
rest-client (~> 1.6.1)
rubyzip
term-ansicolor (~> 1.0.5)
hike (1.2.0)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.13)
railties (~> 3.0)
thor (~> 0.14)
launchy (2.0.5)
addressable (~> 2.2.6)
launchy (2.0.3)
mail (2.3.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
multi_json (1.0.3)
multi_xml (0.2.2)
multipart-post (1.1.3)
net-ldap (0.2.2)
nokogiri (1.4.7)
oa-basic (0.2.6)
oa-core (= 0.2.6)
rest-client (~> 1.6.0)
oa-core (0.2.6)
oa-enterprise (0.2.6)
addressable (= 2.2.4)
net-ldap (~> 0.2.2)
nokogiri (~> 1.4.2)
oa-core (= 0.2.6)
pyu-ruby-sasl (~> 0.0.3.1)
rubyntlm (~> 0.1.1)
oa-more (0.2.6)
multi_json (~> 1.0.0)
oa-core (= 0.2.6)
rest-client (~> 1.6.0)
oa-oauth (0.2.6)
faraday (~> 0.6.1)
multi_json (~> 1.0.0)
multi_xml (~> 0.2.2)
oa-core (= 0.2.6)
oauth (~> 0.4.0)
oauth2 (~> 0.4.1)
oa-openid (0.2.6)
oa-core (= 0.2.6)
rack-openid (~> 1.3.1)
ruby-openid-apps-discovery (~> 1.2.0)
oauth (0.4.5)
oauth2 (0.4.1)
faraday (~> 0.6.1)
multi_json (>= 0.0.5)
omniauth (0.2.6)
oa-basic (= 0.2.6)
oa-core (= 0.2.6)
oa-enterprise (= 0.2.6)
oa-more (= 0.2.6)
oa-oauth (= 0.2.6)
oa-openid (= 0.2.6)
orm_adapter (0.0.5)
pg (0.11.0)
polyglot (0.3.2)
pyu-ruby-sasl (0.0.3.3)
rack (1.3.2)
rack-cache (1.0.2)
rack-cache (1.0.3)
rack (>= 0.4)
rack-mount (0.8.2)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-openid (1.3.1)
rack (>= 1.1.0)
ruby-openid (>= 2.1.8)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
rack (>= 1.0)
rails (3.1.0.rc6)
actionmailer (= 3.1.0.rc6)
actionpack (= 3.1.0.rc6)
activerecord (= 3.1.0.rc6)
activeresource (= 3.1.0.rc6)
activesupport (= 3.1.0.rc6)
rails (3.1.0)
actionmailer (= 3.1.0)
actionpack (= 3.1.0)
activerecord (= 3.1.0)
activeresource (= 3.1.0)
activesupport (= 3.1.0)
bundler (~> 1.0)
railties (= 3.1.0.rc6)
railties (3.1.0.rc6)
actionpack (= 3.1.0.rc6)
activesupport (= 3.1.0.rc6)
railties (= 3.1.0)
railties (3.1.0)
actionpack (= 3.1.0)
activesupport (= 3.1.0)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.8.7)
rdoc (3.9.2)
rest-client (1.6.3)
rake (0.9.2)
rdoc (3.9.4)
rest-client (1.6.7)
mime-types (>= 1.16)
ruby-openid (2.1.8)
ruby-openid-apps-discovery (1.2.0)
ruby-openid (>= 2.1.7)
rubyntlm (0.1.1)
rubyzip (0.9.4)
sass (3.1.7)
sass-rails (3.1.0.rc.6)
actionpack (~> 3.1.0.rc6)
railties (~> 3.1.0.rc6)
sass-rails (3.1.0)
actionpack (~> 3.1.0)
railties (~> 3.1.0)
sass (>= 3.1.4)
sprockets (2.0.0.beta.13)
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
sqlite3 (1.3.4)
term-ansicolor (1.0.6)
thor (0.14.6)
tilt (1.3.2)
tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
turn (0.8.2)
ansi (>= 1.2.2)
tzinfo (0.3.29)
uglifier (1.0.1)
uglifier (1.0.2)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
warden (1.0.5)
Expand All @@ -129,16 +183,17 @@ PLATFORMS
ruby

DEPENDENCIES
coffee-rails (~> 3.1.0.rc)
coffee-rails
devise
gravtastic
grb
haml
heroku
jquery-rails
omniauth
pg
rails (= 3.1.0.rc6)
sass-rails (~> 3.1.0.rc)
rails (~> 3.1)
sass-rails
sqlite3
turn
uglifier
6 changes: 0 additions & 6 deletions app/controllers/application_controller.rb
@@ -1,9 +1,3 @@
class ApplicationController < ActionController::Base
protect_from_forgery

private
helper_method :current_user
def current_user
User.first
end
end
19 changes: 19 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
@@ -0,0 +1,19 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model
@user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)

if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end

def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end

19 changes: 18 additions & 1 deletion app/models/user.rb
Expand Up @@ -2,7 +2,7 @@ class User < ActiveRecord::Base
include Gravtastic
is_gravtastic secure: false, size: 80, default: "identicon"

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable
devise :trackable, :token_authenticatable, :omniauthable

has_many :pairing_sessions_left, foreign_key: :pair_1_id, class_name: "PairingSession"
has_many :pairing_sessions_right, foreign_key: :pair_2_id, class_name: "PairingSession"
Expand All @@ -19,4 +19,21 @@ def find_pairing_session(id)
PairingSession.where("id = ? AND (pair_1_id = ? OR pair_2_id = ?)",
id, self.id, self.id).first
end

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token['extra']['user_hash']
if user = User.find_by_email(data["email"])
user
else # Create a user with a stub password.
User.create(:email => data["email"], :password => Devise.friendly_token[0,20])
end
end

def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["user_hash"]
user.email = data["email"]
end
end
end
end
17 changes: 1 addition & 16 deletions app/views/devise/sessions/new.html.erb
@@ -1,17 +1,2 @@
<h2>Sign in</h2>

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>

<p><%= f.label :password %><br />
<%= f.password_field :password %></p>

<% if devise_mapping.rememberable? -%>
<p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
<% end -%>

<p><%= f.submit "Sign in" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
8 changes: 5 additions & 3 deletions app/views/layouts/application.html.erb
Expand Up @@ -11,11 +11,13 @@
<div id='header'>
<h1><%= link_to "Paired With Us", root_path %></h1>
<ul class='actions'>
<% if user_signed_in? %>
<li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
<li><%= link_to "Edit Registration", edit_user_registration_path%></li>
<li><%= link_to "Login", new_user_session_path %></li>
<li><%= link_to "Register", new_user_registration_path%></li>
<% end %>
</ul>
<% if user_signed_in? %>
<section class='user-name'><%= current_user.email %></section>
<% end %>
</div>
<div id='content'>
<p class="notice"><%= notice %></p>
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/devise.rb
@@ -1,6 +1,13 @@
# Use this hook to configure devise mailer, warden hooks and so forth. The first
# four configuration values can also be set straight in your models.
Devise.setup do |config|

# config.omniauth :github, "4893b2a8e2dcec30b77a", "54153bd7f308431e08d95906288c7a0584fdb01a" # https://github.com/account/applications/2703
config.omniauth :facebook, "258096054220798", "b6c4ba5cdb11bc27a229d2317f7c4fb7", {:scope => 'email, offline_access'}


# https://developers.facebook.com/apps/258096054220798/

# ==> Mailer Configuration
# Configure the e-mail address which will be shown in DeviseMailer.
config.mailer_sender = "coreyhaines@example.com"
Expand Down

0 comments on commit e2ebe82

Please sign in to comment.