Skip to content
Browse files

add facebook oauth

  • Loading branch information...
1 parent a7c5fac commit 604b7b104f4261a6b4d98e2491c2ee02faed5182 @sachinr committed Nov 18, 2011
View
2 Gemfile
@@ -26,6 +26,8 @@ gem 'thin'
gem 'heroku'
gem 'icalendar'
+gem "omniauth-facebook"
+
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
View
18 Gemfile.lock
@@ -56,7 +56,12 @@ GEM
eventmachine (0.12.10)
execjs (1.2.9)
multi_json (~> 1.0)
+ faraday (0.7.5)
+ addressable (~> 2.2.6)
+ multipart-post (~> 1.1.3)
+ rack (>= 1.1.0, < 2)
haml (3.1.3)
+ hashie (1.2.0)
heroku (2.14.0)
launchy (>= 0.3.2)
rest-client (~> 1.6.1)
@@ -87,7 +92,19 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.0.3)
+ multipart-post (1.1.3)
oauth (0.4.5)
+ oauth2 (0.5.1)
+ faraday (~> 0.7.4)
+ multi_json (~> 1.0.3)
+ omniauth (1.0.0)
+ hashie (~> 1.2)
+ rack
+ omniauth-facebook (1.0.0.rc2)
+ omniauth-oauth2 (~> 1.0.0)
+ omniauth-oauth2 (1.0.0)
+ oauth2 (~> 0.5.0)
+ omniauth (~> 1.0)
orm_adapter (0.0.5)
pg (0.11.0)
polyglot (0.3.3)
@@ -185,6 +202,7 @@ DEPENDENCIES
icalendar
jquery-rails
letter_opener
+ omniauth-facebook
pg
rails (= 3.1.1)
ruby-debug19
View
BIN app/assets/images/btn-fb-connect.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
4 app/assets/stylesheets/dashboard.css.scss
@@ -1,3 +1,7 @@
// Place all the styles related to the dashboard controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
+
+p.pull-right{
+ color: #E6E6E6 !important;
+}
View
14 app/controllers/omniauth_callbacks_controller.rb
@@ -0,0 +1,14 @@
+class OmniauthCallbacksController < Devise::OmniauthCallbacksController
+ def facebook
+ @user = User.find_for_facebook_oauth(request.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"] = request.env["omniauth.auth"]
+ redirect_to new_user_registration_url
+ end
+ end
+end
+
View
2 app/helpers/application_helper.rb
@@ -4,7 +4,7 @@ def project_compatibility(project_user_score)
number_of_questions = ProfileQuestion.all.count
high_score = number_of_questions * ProfileAnswer::HIGH_SCORE
medium_score = number_of_questions * (ProfileAnswer::HIGH_SCORE/2)
- score = project_user_score.score
+ score = project_user_score ? project_user_score.score : 0
if score == 0
return 'Average'
View
11 app/models/user.rb
@@ -2,7 +2,7 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable
+ :recoverable, :rememberable, :trackable, :validatable, :omniauthable
has_many :profile_answers, :as => :answerable
has_many :projects, :through => :attendees
@@ -24,6 +24,15 @@ def update_profile_answers(scores)
return true
end
+ def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
+ data = access_token['extra']['raw_info']
+ 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
+
private
def update_project_user_score
View
3 app/views/dashboard/_user_dashboard.html.haml
@@ -14,9 +14,8 @@
= link_to project.name, project
%br
= "#{project.start_date} - #{project.attendees.find_by_user_id(current_user.id).accepted ? 'Accepted' : 'Awaiting acceptance'}"
-
- else
-
+ You don't have any upcoming projects - start browsing the project listing to find something to do
.span5
%h2 Suggested Projects
View
34 app/views/devise/sessions/new.html.erb
@@ -1,22 +1,34 @@
<fieldset>
<legend><%= resource_name.to_s.titleize %> login</legend>
- <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
- <div class="clearfix"><%= f.label :email %><br />
- <div class = "input"><%= f.email_field :email %></div>
- </div>
+ <div class="row">
+ <div class="span8">
+ <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
+ <div class="clearfix"><%= f.label :email %><br />
+ <div class = "input"><%= f.email_field :email %></div>
+ </div>
- <div class="clearfix"><%= f.label :password %><br />
- <div class = "input"><%= f.password_field :password %></div>
- </div>
+ <div class="clearfix"><%= f.label :password %><br />
+ <div class = "input"><%= f.password_field :password %></div>
+ </div>
- <% if devise_mapping.rememberable? -%>
- <div class="input">
- <div class="clearfix"><%= f.check_box :remember_me %> <span>Remember me</span></div>
+ <% if devise_mapping.rememberable? -%>
+ <div class="input">
+ <div class="clearfix"><%= f.check_box :remember_me %> <span>Remember me</span></div>
+ </div>
+ <% end -%>
</div>
- <% end -%>
+ <% if resource.class != Organization -%>
+ <div class"span8">
+ <p>Or sign-in with Facebook</p>
+ <%= link_to(image_tag("/assets/btn-fb-connect.png"), user_omniauth_authorize_path(:facebook)) %>
+ </div>
+ <% end -%>
+
+ </div>
<div class="actions"><%= f.submit "Sign in", :class => 'btn primary' %></div>
<% end %>
</fieldset>
+
<%= render :partial => "devise/shared/links" %>
View
2 config/initializers/devise.rb
@@ -6,6 +6,8 @@
# note that it will be overwritten if you use your own mailer class with default "from" parameter.
config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com"
+ config.omniauth :facebook, "187436761304090", "a7f08628cc9f571f1cc08b7c57c03b13"
+
# Configure the class responsible to send e-mails.
# config.mailer = "Devise::Mailer"
View
2 config/routes.rb
@@ -1,6 +1,6 @@
Helpa::Application.routes.draw do
- devise_for :users
+ devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
devise_for :organizations
namespace :admin do

0 comments on commit 604b7b1

Please sign in to comment.
Something went wrong with that request. Please try again.