Skip to content
This repository has been archived by the owner on Jan 6, 2018. It is now read-only.

Commit

Permalink
can login/logout via meetup.com
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic committed Apr 20, 2011
1 parent 0037779 commit c471991
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 247 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -197,3 +211,4 @@ DEPENDENCIES
pg
rails (= 3.0.7)
rspec-rails (>= 2.0.1)
ruby-debug19
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HomeController < ApplicationController
def index
end

end
23 changes: 22 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/home_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module HomeHelper
end
16 changes: 16 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/views/home/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%h1 Home#index
%p Find me in app/views/home/index.html.haml
20 changes: 18 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
7 changes: 7 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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"

Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20110420182544_add_fields_to_users.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit c471991

Please sign in to comment.