Skip to content

Commit

Permalink
Implemented Users with authentication via OmniAuth/Twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
jcasimir committed Apr 24, 2011
1 parent 5b007f1 commit 6203b68
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 304 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -3,6 +3,7 @@ source 'http://rubygems.org'
gem 'rails', '3.0.6'
gem 'sqlite3'
gem 'haml'
gem 'omniauth'

group :development, :test do
gem 'rspec-rails'
Expand Down
54 changes: 54 additions & 0 deletions Gemfile.lock
Expand Up @@ -29,6 +29,7 @@ GEM
activemodel (= 3.0.6)
activesupport (= 3.0.6)
activesupport (3.0.6)
addressable (2.2.4)
arel (2.0.9)
autotest (4.4.6)
ZenTest (>= 4.4.1)
Expand All @@ -50,6 +51,10 @@ GEM
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
faraday (0.6.1)
addressable (~> 2.2.4)
multipart-post (~> 1.1.0)
rack (>= 1.1.0, < 2)
ffi (1.0.7)
rake (>= 0.8.7)
haml (3.0.25)
Expand All @@ -72,12 +77,56 @@ GEM
treetop (~> 1.4.8)
mime-types (1.16)
mocha (0.9.12)
multi_json (0.0.5)
multipart-post (1.1.0)
net-ldap (0.1.1)
nifty-generators (0.4.6)
nokogiri (1.4.4)
oa-basic (0.2.4)
oa-core (= 0.2.4)
rest-client (~> 1.6.0)
oa-core (0.2.4)
oa-enterprise (0.2.4)
addressable (= 2.2.4)
net-ldap (~> 0.1.1)
nokogiri (~> 1.4.2)
oa-core (= 0.2.4)
pyu-ruby-sasl (~> 0.0.3.1)
rubyntlm (~> 0.1.1)
oa-more (0.2.4)
multi_json (~> 0.0.2)
oa-core (= 0.2.4)
rest-client (~> 1.6.0)
oa-oauth (0.2.4)
faraday (~> 0.6.1)
multi_json (>= 0.0.5)
nokogiri (~> 1.4.2)
oa-core (= 0.2.4)
oauth (~> 0.4.0)
oauth2 (~> 0.4.1)
oa-openid (0.2.4)
oa-core (= 0.2.4)
rack-openid (~> 1.2.0)
ruby-openid-apps-discovery
oauth (0.4.4)
oauth2 (0.4.1)
faraday (~> 0.6.1)
multi_json (>= 0.0.5)
omniauth (0.2.4)
oa-basic (= 0.2.4)
oa-core (= 0.2.4)
oa-enterprise (= 0.2.4)
oa-more (= 0.2.4)
oa-oauth (= 0.2.4)
oa-openid (= 0.2.4)
polyglot (0.3.1)
pyu-ruby-sasl (0.0.3.2)
rack (1.2.2)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-openid (1.2.0)
rack (>= 1.1.0)
ruby-openid (>= 2.1.8)
rack-test (0.5.7)
rack (>= 1.0)
rails (3.0.6)
Expand Down Expand Up @@ -109,6 +158,10 @@ GEM
activesupport (~> 3.0)
railties (~> 3.0)
rspec (~> 2.5.0)
ruby-openid (2.1.8)
ruby-openid-apps-discovery (1.2.0)
ruby-openid (>= 2.1.7)
rubyntlm (0.1.1)
rubyzip (0.9.4)
selenium-webdriver (0.1.4)
childprocess (>= 0.1.7)
Expand Down Expand Up @@ -137,6 +190,7 @@ DEPENDENCIES
jquery-rails
mocha
nifty-generators
omniauth
rails (= 3.0.6)
rspec-rails
sqlite3
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
@@ -1,9 +1,15 @@
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user

def find_resource
class_name = params[:controller].singularize
klass = class_name.camelize.constantize
self.instance_variable_set "@" + class_name, klass.find(params[:id])
end

private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
12 changes: 12 additions & 0 deletions app/controllers/sessions_controller.rb
@@ -0,0 +1,12 @@
class SessionsController < ApplicationController
def create
user = User.find_or_create_by_auth(request.env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_path, :notice => "Logged in as #{current_user.name}"
end

def destroy
session[:user_id] = nil
redirect_to root_path, :notice => "Goodbye!"
end
end
2 changes: 2 additions & 0 deletions app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
module SessionsHelper
end
4 changes: 4 additions & 0 deletions app/models/company.rb
Expand Up @@ -2,4 +2,8 @@ class Company < ActiveRecord::Base
attr_accessible :name
validates_presence_of :name
include Contact

def to_s
name
end
end
11 changes: 11 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,11 @@
class User < ActiveRecord::Base

def self.find_or_create_by_auth(auth_data)
user = self.find_or_create_by_provider_and_uid(auth_data["provider"], auth_data["uid"])
if user.name != auth_data["user_info"]["name"]
user.name = auth_data["user_info"]["name"]
user.save
end
return user
end
end
8 changes: 8 additions & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -14,6 +14,14 @@
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<div id='account'>
<% if current_user %>
<span>Welcome, <%= current_user.name %></span>
<%= link_to "logout", logout_path, :id => "login" %>
<% else %>
<%= link_to "login", login_path, :id => "logout" %>
<% end %>
</div>
<%= content_tag :h1, yield(:title) if show_title? %>
<%= yield %>
</div>
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/omniauth.rb
@@ -0,0 +1,3 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, "EZYxQSqP0j35QWqoV0kUg", "IToKT8jdWZEhEH60wFL94HGf4uoGE1SqFUrZUR34M4"
end
69 changes: 5 additions & 64 deletions config/routes.rb
@@ -1,66 +1,7 @@
JSContact::Application.routes.draw do
resources :companies

resources :email_addresses

resources :phone_numbers

resources :people

# The priority is based upon order of creation:
# first created -> highest priority.

# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action

# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)

# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end

# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# 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"

# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
resources :companies, :email_addresses, :phone_numbers, :people
match '/auth/:provider/callback', :to => 'sessions#create'
match "/login" => redirect("/auth/twitter"), :as => :login
match "/logout" => "sessions#destroy", :as => :logout
root :to => "companies#index"
end
15 changes: 15 additions & 0 deletions db/migrate/20110424163240_create_users.rb
@@ -0,0 +1,15 @@
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :provider
t.string :uid
t.string :name

t.timestamps
end
end

def self.down
drop_table :users
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110420161636) do
ActiveRecord::Schema.define(:version => 20110424163240) do

create_table "companies", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -41,4 +41,12 @@
t.string "contact_type"
end

create_table "users", :force => true do |t|
t.string "provider"
t.string "uid"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

end

0 comments on commit 6203b68

Please sign in to comment.