Permalink
Browse files

Implemented Users with authentication via OmniAuth/Twitter

  • Loading branch information...
1 parent 5b007f1 commit 6203b682957fe3c2bef38328414d321d6d417434 @jcasimir committed Apr 24, 2011
View
@@ -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'
View
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -137,6 +190,7 @@ DEPENDENCIES
jquery-rails
mocha
nifty-generators
+ omniauth
rails (= 3.0.6)
rspec-rails
sqlite3
@@ -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
@@ -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
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
View
@@ -2,4 +2,8 @@ class Company < ActiveRecord::Base
attr_accessible :name
validates_presence_of :name
include Contact
+
+ def to_s
+ name
+ end
end
View
@@ -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
@@ -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>
@@ -0,0 +1,3 @@
+Rails.application.config.middleware.use OmniAuth::Builder do
+ provider :twitter, "EZYxQSqP0j35QWqoV0kUg", "IToKT8jdWZEhEH60wFL94HGf4uoGE1SqFUrZUR34M4"
+end
View
@@ -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
@@ -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
View
@@ -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"
@@ -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
Oops, something went wrong.

0 comments on commit 6203b68

Please sign in to comment.