Skip to content

Commit

Permalink
add facebook oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinr committed Nov 17, 2011
1 parent a7c5fac commit 604b7b1
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ gem 'thin'
gem 'heroku' gem 'heroku'
gem 'icalendar' gem 'icalendar'


gem "omniauth-facebook"

# To use ActiveModel has_secure_password # To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0' # gem 'bcrypt-ruby', '~> 3.0.0'


Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ GEM
eventmachine (0.12.10) eventmachine (0.12.10)
execjs (1.2.9) execjs (1.2.9)
multi_json (~> 1.0) 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) haml (3.1.3)
hashie (1.2.0)
heroku (2.14.0) heroku (2.14.0)
launchy (>= 0.3.2) launchy (>= 0.3.2)
rest-client (~> 1.6.1) rest-client (~> 1.6.1)
Expand Down Expand Up @@ -87,7 +92,19 @@ GEM
treetop (~> 1.4.8) treetop (~> 1.4.8)
mime-types (1.17.2) mime-types (1.17.2)
multi_json (1.0.3) multi_json (1.0.3)
multipart-post (1.1.3)
oauth (0.4.5) 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) orm_adapter (0.0.5)
pg (0.11.0) pg (0.11.0)
polyglot (0.3.3) polyglot (0.3.3)
Expand Down Expand Up @@ -185,6 +202,7 @@ DEPENDENCIES
icalendar icalendar
jquery-rails jquery-rails
letter_opener letter_opener
omniauth-facebook
pg pg
rails (= 3.1.1) rails (= 3.1.1)
ruby-debug19 ruby-debug19
Expand Down
Binary file added app/assets/images/btn-fb-connect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/dashboard.css.scss
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,7 @@
// Place all the styles related to the dashboard controller here. // Place all the styles related to the dashboard controller here.
// They will automatically be included in application.css. // They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/ // You can use Sass (SCSS) here: http://sass-lang.com/

p.pull-right{
color: #E6E6E6 !important;
}
14 changes: 14 additions & 0 deletions app/controllers/omniauth_callbacks_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def project_compatibility(project_user_score)
number_of_questions = ProfileQuestion.all.count number_of_questions = ProfileQuestion.all.count
high_score = number_of_questions * ProfileAnswer::HIGH_SCORE high_score = number_of_questions * ProfileAnswer::HIGH_SCORE
medium_score = number_of_questions * (ProfileAnswer::HIGH_SCORE/2) 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 if score == 0
return 'Average' return 'Average'
Expand Down
11 changes: 10 additions & 1 deletion app/models/user.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable, :omniauthable


has_many :profile_answers, :as => :answerable has_many :profile_answers, :as => :answerable
has_many :projects, :through => :attendees has_many :projects, :through => :attendees
Expand All @@ -24,6 +24,15 @@ def update_profile_answers(scores)
return true return true
end 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 private


def update_project_user_score def update_project_user_score
Expand Down
3 changes: 1 addition & 2 deletions app/views/dashboard/_user_dashboard.html.haml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
= link_to project.name, project = link_to project.name, project
%br %br
= "#{project.start_date} - #{project.attendees.find_by_user_id(current_user.id).accepted ? 'Accepted' : 'Awaiting acceptance'}" = "#{project.start_date} - #{project.attendees.find_by_user_id(current_user.id).accepted ? 'Accepted' : 'Awaiting acceptance'}"

- else - else

You don't have any upcoming projects - start browsing the project listing to find something to do


.span5 .span5
%h2 Suggested Projects %h2 Suggested Projects
Expand Down
34 changes: 23 additions & 11 deletions app/views/devise/sessions/new.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -1,22 +1,34 @@
<fieldset> <fieldset>
<legend><%= resource_name.to_s.titleize %> login</legend> <legend><%= resource_name.to_s.titleize %> login</legend>


<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %> <div class="row">
<div class="clearfix"><%= f.label :email %><br /> <div class="span8">
<div class = "input"><%= f.email_field :email %></div> <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
</div> <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="clearfix"><%= f.label :password %><br />
<div class = "input"><%= f.password_field :password %></div> <div class = "input"><%= f.password_field :password %></div>
</div> </div>


<% if devise_mapping.rememberable? -%> <% if devise_mapping.rememberable? -%>
<div class="input"> <div class="input">
<div class="clearfix"><%= f.check_box :remember_me %> <span>Remember me</span></div> <div class="clearfix"><%= f.check_box :remember_me %> <span>Remember me</span></div>
</div>
<% end -%>
</div> </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> <div class="actions"><%= f.submit "Sign in", :class => 'btn primary' %></div>
<% end %> <% end %>
</fieldset> </fieldset>

<%= render :partial => "devise/shared/links" %> <%= render :partial => "devise/shared/links" %>
2 changes: 2 additions & 0 deletions config/initializers/devise.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# note that it will be overwritten if you use your own mailer class with default "from" parameter. # 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.mailer_sender = "please-change-me-at-config-initializers-devise@example.com"


config.omniauth :facebook, "187436761304090", "a7f08628cc9f571f1cc08b7c57c03b13"

# Configure the class responsible to send e-mails. # Configure the class responsible to send e-mails.
# config.mailer = "Devise::Mailer" # config.mailer = "Devise::Mailer"


Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
Helpa::Application.routes.draw do Helpa::Application.routes.draw do


devise_for :users devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
devise_for :organizations devise_for :organizations


namespace :admin do namespace :admin do
Expand Down

0 comments on commit 604b7b1

Please sign in to comment.