Permalink
Browse files

can login/logout via meetup.com

  • Loading branch information...
1 parent 0037779 commit c4719918c4d02c496b8496859193f7132da252db @drnic committed Apr 20, 2011
View
@@ -15,7 +15,7 @@ gem 'pg'
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
-# gem 'ruby-debug19', :require => 'ruby-debug'
+gem 'ruby-debug19', :require => 'ruby-debug'
# Bundle the extra gems:
# gem 'bj'
View
@@ -29,6 +29,7 @@ GEM
activesupport (= 3.0.7)
activesupport (3.0.7)
addressable (2.2.5)
+ archive-tar-minitar (0.5.2)
arel (2.0.9)
builder (2.1.2)
capybara (0.4.1.2)
@@ -43,6 +44,7 @@ GEM
celerity (0.8.9)
childprocess (0.1.8)
ffi (~> 1.0.6)
+ columnize (0.3.2)
cucumber (0.10.2)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
@@ -77,6 +79,8 @@ GEM
thor (~> 0.14.4)
json (1.5.1)
json_pure (1.5.1)
+ linecache19 (0.5.11)
+ ruby_core_source (>= 0.1.4)
mail (2.2.17)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -166,9 +170,19 @@ GEM
activesupport (~> 3.0)
railties (~> 3.0)
rspec (~> 2.5.0)
+ ruby-debug-base19 (0.11.24)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
ruby-openid (2.1.8)
ruby-openid-apps-discovery (1.2.0)
ruby-openid (>= 2.1.7)
+ ruby_core_source (0.1.4)
+ archive-tar-minitar (>= 0.5.2)
rubyntlm (0.1.1)
rubyzip (0.9.4)
selenium-webdriver (0.1.4)
@@ -197,3 +211,4 @@ DEPENDENCIES
pg
rails (= 3.0.7)
rspec-rails (>= 2.0.1)
+ ruby-debug19
@@ -1,3 +1,11 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ helper_method :current_user
+
+ private
+
+ def current_user
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
+ end
end
@@ -0,0 +1,5 @@
+class HomeController < ApplicationController
+ def index
+ end
+
+end
@@ -1,7 +1,28 @@
class SessionsController < ApplicationController
- def callback
+ def old_callback
auth # Do what you want with the auth hash!
end
+ def callback
+ if user = User.find_by_provider_and_uid(auth["provider"], auth["uid"])
+ user.update_with_meetup(auth)
+ user.save!
+ else
+ user = User.create_with_meetup(auth)
+ end
+ session[:user_id] = user.id
+ redirect_to root_url, :notice => "Signed in!"
+ end
+
+ def destroy
+ session[:user_id] = nil
+ redirect_to root_url, :notice => "Signed out!"
+ end
+
+ def failure
+ raise auth.to_yaml
+ redirect_to root_url, :error => "Failed to authenticate: #{params[:message]}"
+ end
+
def auth; request.env['omniauth.auth'] end
end
@@ -0,0 +1,2 @@
+module HomeHelper
+end
View
@@ -1,2 +1,18 @@
class User < ActiveRecord::Base
+ def self.create_with_meetup(auth)
+ user = create! do |user|
+ user.provider = auth["provider"]
+ user.uid = auth["uid"]
+ user.update_with_omniauth(auth)
+ end
+ end
+
+ # Sets User attributes based on omniauth/meetup.com fields
+ # NOTE: does not save the record
+ def update_with_meetup(auth)
+ self.name = auth["user_info"]["name"]
+ self.location = auth["user_info"]["location"]
+ self.image_url = auth["user_info"]["image"]
+ self.profile_url = auth["user_info"]["urls"]["profile"]
+ end
end
@@ -0,0 +1,2 @@
+%h1 Home#index
+%p Find me in app/views/home/index.html.haml
@@ -7,8 +7,24 @@
<%= csrf_meta_tag %>
</head>
<body>
-
-<%= yield %>
+ <style type="text/css" media="screen">
+ .user-image {
+ width: 48px;
+ }
+ </style>
+ <% if current_user %>
+ <%= image_tag current_user.image_url, :class => "user-image" %>
+ <%= current_user.name %> |
+ <%= link_to "Sign Out", signout_path %>
+ <% else %>
+ <%= link_to "Sign in with Meetup.com", "/auth/meetup" %>
+ <% end %>
+
+ <% flash.each do |name, msg| %>
+ <pre><%= {name: msg}.inspect %></pre>
+ <%= content_tag :div, msg, id: "flash_#{name}" %>
+ <% end %>
+ <%= yield %>
</body>
</html>
@@ -0,0 +1,7 @@
+Rails.application.config.middleware.use OmniAuth::Builder do
+ if Rails.env.production?
+ provider :meetup, '5bh4rgdj9937o65aoeots5n2ro', '8eoeuefatomrhfjsk90k15ghlf' # http://awards.svruby.com
+ else
+ provider :meetup, 'jr86pidmke1mr51ldq491oepln', 'd07p50nq5j4h9r0l58jc1hfoqn' # http://svruby-awards.dev
+ end
+end
View
@@ -1,5 +1,9 @@
SvrubyAwards::Application.routes.draw do
match '/auth/:provider/callback', :to => 'sessions#callback'
+ match '/auth/failure', :to => 'sessions#failure'
+ match "/signout" => "sessions#destroy", :as => :signout
+
+ root :to => "home#index"
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -48,9 +52,6 @@
# resources :products
# end
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- # root :to => "welcome#index"
# See how all your routes lay out with "rake routes"
@@ -0,0 +1,13 @@
+class AddFieldsToUsers < ActiveRecord::Migration
+ def self.up
+ add_column :users, :image_url, :string
+ add_column :users, :location, :string
+ add_column :users, :profile_url, :string
+ end
+
+ def self.down
+ remove_column :users, :profile_url
+ remove_column :users, :location
+ remove_column :users, :image_url
+ end
+end
View
@@ -10,14 +10,17 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110420180328) do
+ActiveRecord::Schema.define(:version => 20110420182544) do
create_table "users", :force => true do |t|
t.string "provider"
t.string "uid"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "image_url"
+ t.string "location"
+ t.string "profile_url"
end
end
Oops, something went wrong.

0 comments on commit c471991

Please sign in to comment.