Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from UWE-Ruby/omniauth
Browse files Browse the repository at this point in the history
Omniauth
  • Loading branch information
ivanoats committed Mar 4, 2012
2 parents fab32a7 + 0004bfd commit 67f49f8
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 2 deletions.
7 changes: 6 additions & 1 deletion Gemfile
Expand Up @@ -6,7 +6,8 @@ gem 'rails', '3.2.0'
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

gem 'omniauth'
gem 'omniauth-twitter'

# Gems used only for assets and not required
# in production environments by default.
Expand All @@ -23,6 +24,10 @@ end
gem 'jquery-rails'
gem 'client_side_validations'

group :development do
gem 'capistrano'
end

group :test, :development do
gem "rspec-rails", "~> 2.6"
gem "capybara"
Expand Down
27 changes: 27 additions & 0 deletions Gemfile.lock
Expand Up @@ -31,6 +31,12 @@ GEM
addressable (2.2.6)
arel (3.0.0)
builder (3.0.0)
capistrano (2.11.2)
highline
net-scp (>= 1.0.0)
net-sftp (>= 2.0.0)
net-ssh (>= 2.0.14)
net-ssh-gateway (>= 1.1.0)
capybara (1.1.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
Expand Down Expand Up @@ -59,6 +65,8 @@ GEM
factory_girl (2.5.0)
activesupport
ffi (1.0.11)
hashie (1.2.0)
highline (1.6.11)
hike (1.2.1)
i18n (0.6.0)
journey (1.0.1)
Expand All @@ -74,7 +82,23 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.0.4)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
net-sftp (2.0.5)
net-ssh (>= 2.0.9)
net-ssh (2.3.0)
net-ssh-gateway (1.1.0)
net-ssh (>= 1.99.1)
nokogiri (1.5.0)
oauth (0.4.5)
omniauth (1.0.2)
hashie (~> 1.2)
rack
omniauth-oauth (1.0.0)
oauth
omniauth (~> 1.0)
omniauth-twitter (0.0.7)
omniauth-oauth (~> 1.0)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.1)
Expand Down Expand Up @@ -146,6 +170,7 @@ PLATFORMS
ruby

DEPENDENCIES
capistrano
capybara
capybara-webkit
client_side_validations
Expand All @@ -154,6 +179,8 @@ DEPENDENCIES
factory_girl
jquery-rails
launchy
omniauth
omniauth-twitter
rails (= 3.2.0)
rspec-rails (~> 2.6)
sass-rails (~> 3.2.3)
Expand Down
96 changes: 96 additions & 0 deletions Vagrantfile
@@ -0,0 +1,96 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant::Config.run do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "base"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
# config.vm.box_url = "http://domain.com/path/to/above.box"

# Boot with a GUI so you can see the screen. (Default is headless)
# config.vm.boot_mode = :gui

# Assign this VM to a host-only network IP, allowing you to access it
# via the IP. Host-only networks can talk to the host machine as well as
# any other machines on the same network, but cannot be accessed (through this
# network interface) by any external networks.
# config.vm.network :hostonly, "33.33.33.10"

# Assign this VM to a bridged network, allowing you to connect directly to a
# network using the host's network device. This makes the VM appear as another
# physical device on your network.
# config.vm.network :bridged

# Forward a port from the guest to the host, which allows for outside
# computers to access the VM, whereas host only networking does not.
config.vm.forward_port 80, 8080

# Share an additional folder to the guest VM. The first argument is
# an identifier, the second is the path on the guest to mount the
# folder, and the third is the path on the host to the actual folder.
# config.vm.share_folder "v-data", "/vagrant_data", "../data"

# Enable provisioning with Puppet stand alone. Puppet manifests
# are contained in a directory path relative to this Vagrantfile.
# You will need to create the manifests directory and a manifest in
# the file base.pp in the manifests_path directory.
#
# An example Puppet manifest to provision the message of the day:
#
# # group { "puppet":
# # ensure => "present",
# # }
# #
# # File { owner => 0, group => 0, mode => 0644 }
# #
# # file { '/etc/motd':
# # content => "Welcome to your Vagrant-built virtual machine!
# # Managed by Puppet.\n"
# # }
#
# config.vm.provision :puppet do |puppet|
# puppet.manifests_path = "manifests"
# puppet.manifest_file = "base.pp"
# end

# Enable provisioning with chef solo, specifying a cookbooks path (relative
# to this Vagrantfile), and adding some recipes and/or roles.
#
# config.vm.provision :chef_solo do |chef|
# chef.cookbooks_path = "cookbooks"
# chef.add_recipe "mysql"
# chef.add_role "web"
#
# # You may also specify custom JSON attributes:
# chef.json = { :mysql_password => "foo" }
# end

# Enable provisioning with chef server, specifying the chef server URL,
# and the path to the validation key (relative to this Vagrantfile).
#
# The Opscode Platform uses HTTPS. Substitute your organization for
# ORGNAME in the URL and validation key.
#
# If you have your own Chef Server, use the appropriate URL, which may be
# HTTP instead of HTTPS depending on your configuration. Also change the
# validation key to validation.pem.
#
# config.vm.provision :chef_client do |chef|
# chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
# chef.validation_key_path = "ORGNAME-validator.pem"
# end
#
# If you're using the Opscode platform, your validator client is
# ORGNAME-validator, replacing ORGNAME with your organization name.
#
# IF you have your own Chef Server, the default validation client name is
# chef-validator, unless you changed the configuration.
#
# chef.validation_client_name = "ORGNAME-validator"
end
3 changes: 3 additions & 0 deletions app/assets/javascripts/sessions.js.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/sessions.css.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
12 changes: 12 additions & 0 deletions app/controllers/sessions_controller.rb
@@ -0,0 +1,12 @@
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url, notice: "Signed In!"
end

def destroy
session[:user_id] = nil
end
end
2 changes: 2 additions & 0 deletions app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
module SessionsHelper
end
9 changes: 9 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,9 @@
class User < ActiveRecord::Base
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
end
end
end
8 changes: 8 additions & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -7,6 +7,14 @@
<%= csrf_meta_tags %>
</head>
<body>
<% flash.each do |key, value| %>
<div class="flash" id="<%= key %>">
<%= value %>
</div>
<% end %>
<div id="user_nav">
<%= link_to "Sign in with Twitter", "/auth/twitter" %>
</div>

<%= yield %>

Expand Down
2 changes: 2 additions & 0 deletions app/views/sessions/destroy.html.erb
@@ -0,0 +1,2 @@
<h3>Buh-bye</h3>
<p>You are now logged out!</p>
3 changes: 3 additions & 0 deletions config/initializers/omniauth.rb
@@ -0,0 +1,3 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, 'wrJfEYtVLkED7W36065Iw', 'QfeSQ552Dv9JCrtTAizmKd6DfCo5QV3L6lvr9eUtko'
end
3 changes: 3 additions & 0 deletions config/routes.rb
@@ -1,6 +1,9 @@
Recipes::Application.routes.draw do
resources :recipes

match "/logout" => "sessions#destroy"
match "/auth/:provider/callback" => "sessions#create"

root to: 'recipes#index'
# The priority is based upon order of creation:
# first created -> highest priority.
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20120205055615_create_users.rb
@@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :provider
t.string :uid
t.string :name

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

ActiveRecord::Schema.define(:version => 20120131045212) do
ActiveRecord::Schema.define(:version => 20120205055615) do

create_table "recipes", :force => true do |t|
t.string "name"
Expand All @@ -20,4 +20,12 @@
t.datetime "updated_at", :null => false
end

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

end
5 changes: 5 additions & 0 deletions spec/controllers/sessions_controller_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe SessionsController do

end
15 changes: 15 additions & 0 deletions spec/helpers/sessions_helper_spec.rb
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the SessionsHelper. For example:
#
# describe SessionsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe SessionsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe User do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 67f49f8

Please sign in to comment.