Permalink
Browse files

adding create_from_provider

  • Loading branch information...
1 parent 5a82b69 commit 1333abbeda5f4920a7c0a1d9bae317207432945c @NoamB NoamB committed Mar 11, 2011
View
6 app/controllers/application_controller.rb
@@ -13,11 +13,13 @@ class ApplicationController < ActionController::Base
config.twitter.key = "eYVNBjBDi33aa9GkA3w"
config.twitter.secret = "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8"
- config.twitter.callback_url = "http://0.0.0.0:3000/oauth/twitter_callback"
+ config.twitter.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=twitter"
+ config.twitter.user_info_mapping = {:email => "screen_name"}
config.facebook.key = "34cebc81c08a521bc66e212f947d73ec"
config.facebook.secret = "5b458d179f61d4f036ee66a497ffbcd0"
- config.facebook.callback_url = "http://0.0.0.0:3000/oauth/facebook_callback"
+ config.facebook.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=facebook"
+ config.facebook.user_info_mapping = {:email => "name"}
end
before_filter :require_login, :except => [:not_authenticated]
View
53 app/controllers/oauths_controller.rb
@@ -1,57 +1,26 @@
class OauthsController < ApplicationController
skip_before_filter :require_login
- # sends the user on a trip to twitter,
+ # sends the user on a trip to the provider,
# and after authorizing there back to the callback url.
- def twitter
- auth_at_provider(:twitter)
+ def oauth
+ auth_at_provider(params[:provider])
end
- def twitter_callback
- if @user = login_from_access_token(:twitter)
- redirect_to root_path, :notice => "Logged in from Twitter!"
+ def callback
+ provider = params[:provider]
+ if @user = login_from_access_token(provider)
+ redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
else
begin
- @user_hash = get_user_hash(:twitter)
- p @user_hash
- @user = User.create!(:email => @user_hash[:user_info]["screen_name"],
- :providers_attributes => [{
- :provider => :twitter,
- :uid => @user_hash[:uid]
- }])
+ @user = create_from_provider!(provider)
@user.activate!
reset_session # protect from session fixation attack
login_user(@user)
- redirect_to root_path, :notice => "Logged in from Twitter!"
- #rescue
- # redirect_to root_path, :alert => "Failed to login from Twitter!"
+ redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
+ rescue
+ redirect_to root_path, :alert => "Failed to login from #{provider.titleize}!"
end
end
end
-
- def facebook
- auth_at_provider(:facebook)
- end
-
- def facebook_callback
- if @user = login_from_access_token(:facebook)
- redirect_to root_path, :notice => "Logged in from Facebook!"
- else
- begin
- @user_hash = get_user_hash(:facebook)
- p @user_hash
- @user = User.create!(:email => @user_hash[:user_info]["name"],
- :providers_attributes => [{
- :provider => :facebook,
- :uid => @user_hash[:uid]
- }])
- @user.activate!
- reset_session # protect from session fixation attack
- login_user(@user)
- redirect_to root_path, :notice => "Logged in from Facebook!"
- #rescue
- # redirect_to root_path, :alert => "Failed to login from Facebook!"
- end
- end
- end
end
View
4 app/views/layouts/application.html.erb
@@ -16,8 +16,8 @@
<%= link_to "Register", new_user_path %> |
<%= link_to "Login", :login %> |
<%= link_to 'Login from HTTP', login_from_http_users_path %> |
- <%= link_to 'Login with Twitter', twitter_oauth_path %> |
- <%= link_to 'Login with Facebook', facebook_oauth_path %>
+ <%= link_to 'Login with Twitter', auth_at_provider_path(:provider => :twitter) %> |
+ <%= link_to 'Login with Facebook', auth_at_provider_path(:provider => :facebook) %>
<% end %>
</div>
<% if current_user %>
View
18 app/views/users/_form.html.erb
@@ -15,14 +15,16 @@
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
- <div class="field">
- <%= f.label :password %><br />
- <%= f.password_field :password %>
- </div>
- <div class="field">
- <%= f.label :password_confirmation %><br />
- <%= f.password_field :password_confirmation %>
- </div>
+ <% unless @user.external? %>
+ <div class="field">
+ <%= f.label :password %><br />
+ <%= f.password_field :password %>
+ </div>
+ <div class="field">
+ <%= f.label :password_confirmation %><br />
+ <%= f.password_field :password_confirmation %>
+ </div>
+ <% end %>
<div class="actions">
<%= f.submit %>
</div>
View
2 app/views/users/index.html.erb
@@ -3,7 +3,7 @@
<p id="alert"><%= alert %></p>
<table>
<tr>
- <th>Email</th>
+ <th>User</th>
<th></th>
<th></th>
<th></th>
View
2 app/views/users/show.html.erb
@@ -1,7 +1,7 @@
<p id="notice"><%= notice %></p>
<p>
- <b>Email:</b>
+ <b>User:</b>
<%= @user.email %>
</p>
View
6 config/routes.rb
@@ -17,11 +17,9 @@
match 'logout' => 'user_sessions#destroy', :as => :logout
resource :oauth do
- get :twitter
- get :twitter_callback
- get :facebook
- get :facebook_callback
+ get :callback
end
+ match "oauth/:provider" => "oauths#oauth", :as => :auth_at_provider
# The priority is based upon order of creation:
# first created -> highest priority.

0 comments on commit 1333abb

Please sign in to comment.