Skip to content

Commit

Permalink
adding facebook login functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Omers committed Mar 18, 2014
1 parent 504102e commit e6c35df
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem 'rails'
gem 'pg'
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
gem "has_mailbox"
gem 'fastercsv' # Only required on Ruby 1.8 and below
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'
Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ GEM
erubis (2.7.0)
execjs (1.2.13)
multi_json (~> 1.0)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
fastercsv (1.5.4)
haml (3.1.4)
has_mailbox (1.5.5)
Expand All @@ -111,11 +113,14 @@ GEM
highline (1.6.9)
hike (1.2.1)
hpricot (0.8.5)
httpauth (0.2.1)
i18n (0.6.0)
jquery-rails (1.0.19)
railties (~> 3.0)
thor (~> 0.14)
json (1.4.6)
jwt (0.1.6)
multi_json (>= 1.0)
kaminari (0.13.0)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
Expand All @@ -131,9 +136,21 @@ GEM
polyamorous (~> 0.5.0)
mime-types (1.17.2)
multi_json (1.0.4)
multipart-post (2.0.0)
oauth2 (0.8.1)
faraday (~> 0.8)
httpauth (~> 0.1)
jwt (~> 0.1.4)
multi_json (~> 1.0)
rack (~> 1.2)
omniauth (1.0.1)
hashie (~> 1.2)
rack
omniauth-facebook (1.6.0)
omniauth-oauth2 (~> 1.1)
omniauth-oauth2 (1.1.1)
oauth2 (~> 0.8.0)
omniauth (~> 1.0)
orm_adapter (0.0.5)
pg (0.12.2)
polyamorous (0.5.0)
Expand Down Expand Up @@ -223,6 +240,7 @@ DEPENDENCIES
meta_search
metropoli!
omniauth
omniauth-facebook
paperclip!
pg
rails
Expand Down
22 changes: 21 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_mailbox

Expand All @@ -22,6 +22,26 @@ class User < ActiveRecord::Base
def role?(role)
!!self.roles.find_by_name(role.to_s)
end

def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"]
end
end
end

# Setting facebook log in functionality
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:email => data.email).first
user
else
# Create a user with a stub password.
user = User.create!(:username => data.username ? data.username : data.nickname , :email => data.email, :password => Devise.friendly_token[0,20])
end
end

end
# == Schema Information
#
Expand Down
14 changes: 9 additions & 5 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
<ul class="nav">
<li><a href="#about">Descubre <span>Otros perfiles</span></a></li>
</ul>
<div id="user-widget">
<% if current_user %>
Welcome <%= current_user.username %> !
<%= link_to('Sign Out', destroy_user_session_path, :method=>'delete') %>
<% else %>
<%= link_to "Sign in with Facebook", "users/auth/facebook", id: "sign_in" %>
<% end %>
</div>
<ul class="sub-nav">
<ul class="nav">
<% if user_signed_in? %>
Expand All @@ -59,18 +67,14 @@
</li>
<% else %>
<li>
<%= link_to('Login', new_user_session_path) %>
<%= link_to('Login', new_user_registration_path) %>
</li>
<% end %>

<% if user_signed_in? && !current_user.profile.nil? %>
<li>
<%= link_to('Tu cuenta', my_profile_path(:id => current_user.profile.id)) %>
</li>
<% else %>
<li>
<%= link_to('Regístrate', new_user_registration_path) %>
</li>
<% end %>
</ul>
</ul>
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :delete
require "omniauth-facebook"
config.omniauth :facebook, "1432521453659398", "62efcda6545e20dbb80a2a381fc259f3"

This comment has been minimized.

Copy link
@poguez

poguez Apr 26, 2014

Member

Amigo hay que sacar informacion de tokens. Hay un Gem que se llama figaro que nos puede ayudar.


# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
metropoli_for :cities, :states, :countries
resources :coworker_requests
resources :profiles
#Adding routes facebook auth
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
# Override devise default routes for login, logout and signup
devise_for :users do
get '/login' => "devise/sessions#new", :as => :new_user_session
Expand Down

0 comments on commit e6c35df

Please sign in to comment.