Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributions #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Users::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
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"

# ==> 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