Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Basic user login, plus public and private pages.

- Sessions handled by `SessionsController`
- Singluar resource `dashboard` for logged in user
- Users public profiles surved up by `UsersController` with a shorted
  vanity url (i.e. http://hostname/username)
  • Loading branch information...
commit 98fa98c703e762f542c6eed5aaab531be935604f 1 parent dc204f6
@clowder clowder authored
View
4 Gemfile
@@ -4,6 +4,8 @@ gem 'rails', '3.2.9'
gem 'pg'
gem 'unicorn'
gem 'foreman'
+gem 'omniauth'
+gem 'omniauth-github'
# Gems used only for assets and not required
# in production environments by default.
@@ -21,4 +23,4 @@ gem 'jquery-rails'
gem "less-rails"
gem "twitter-bootstrap-rails"
-gem 'simple_form'
+gem 'simple_form'
View
24 Gemfile.lock
@@ -41,15 +41,21 @@ GEM
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
+ faraday (0.8.4)
+ multipart-post (~> 1.1)
foreman (0.60.2)
thor (>= 0.13.6)
+ hashie (1.2.0)
hike (1.2.1)
+ httpauth (0.2.0)
i18n (0.6.1)
journey (1.0.4)
jquery-rails (2.1.3)
railties (>= 3.1.0, < 5.0)
thor (~> 0.14)
json (1.7.5)
+ jwt (0.1.5)
+ multi_json (>= 1.0)
kgio (2.7.4)
less (2.2.2)
commonjs (~> 0.2.6)
@@ -63,6 +69,22 @@ GEM
treetop (~> 1.4.8)
mime-types (1.19)
multi_json (1.3.7)
+ multipart-post (1.1.5)
+ oauth2 (0.8.0)
+ faraday (~> 0.8)
+ httpauth (~> 0.1)
+ jwt (~> 0.1.4)
+ multi_json (~> 1.0)
+ rack (~> 1.2)
+ omniauth (1.1.1)
+ hashie (~> 1.2)
+ rack
+ omniauth-github (1.0.3)
+ omniauth (~> 1.0)
+ omniauth-oauth2 (~> 1.1)
+ omniauth-oauth2 (1.1.1)
+ oauth2 (~> 0.8.0)
+ omniauth (~> 1.0)
pg (0.14.1)
polyglot (0.3.3)
rack (1.4.1)
@@ -132,6 +154,8 @@ DEPENDENCIES
foreman
jquery-rails
less-rails
+ omniauth
+ omniauth-github
pg
rails (= 3.2.9)
sass-rails (~> 3.2.3)
View
3  app/assets/stylesheets/dashboard.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the dashboard controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
6 app/assets/stylesheets/site.css.scss
@@ -24,10 +24,12 @@ h1 span{
font-size: 24px;
letter-spacing:0;
}
+
.forkme{
position: fixed;
display:block;
- top: 0;
+ top: 40px;
+ z-index: -1;
right: 0;
}
@@ -38,4 +40,4 @@ h1 span{
footer{
font-size:14px;
margin:20px 0;
-}
+}
View
3  app/assets/stylesheets/users.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the users controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
18 app/controllers/application_controller.rb
@@ -1,3 +1,21 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ helper_method :current_user, :logged_in?
+
+ private
+ def ensure_logged_in
+ unless logged_in?
+ flash[:notice] = "You must be logged in to view this content."
+ redirect_to root_url() # back or home might be more useful
+ end
+ end
+
+ def current_user
+ @current_user ||= User.find_by_id(session[:user_id])
+ end
+
+ def logged_in?
+ !!current_user
+ end
end
View
7 app/controllers/dashboards_controller.rb
@@ -0,0 +1,7 @@
+class DashboardsController < ApplicationController
+ before_filter :ensure_logged_in
+
+ def show
+ render :show, :locals => { :user => current_user }
+ end
+end
View
24 app/controllers/sessions_controller.rb
@@ -0,0 +1,24 @@
+class SessionsController < ApplicationController
+ def new
+ redirect_to "/auth/#{ Rails.application.config.default_provider }"
+ end
+
+ def create
+ auth_hash = request.env['omniauth.auth']
+ user = User.find_by_auth_hash(auth_hash) ||
+ User.create_from_auth_hash(auth_hash)
+
+ session[:user_id] = user.id
+ redirect_to dashboard_path()
+ end
+
+ def destroy
+ session.delete(:user_id)
+ redirect_to root_path()
+ end
+
+ def failure
+ flash[:notice] = params[:message]
+ redirect_to root_path()
+ end
+end
View
6 app/controllers/users_controller.rb
@@ -0,0 +1,6 @@
+class UsersController < ApplicationController
+ def show
+ user = User.find(params[:id])
+ render :show, :locals => { :user => user }
+ end
+end
View
30 app/models/user.rb
@@ -0,0 +1,30 @@
+class User < ActiveRecord::Base
+ attr_accessible :uid, :provider, :nickname, :email
+
+ def self.create_from_auth_hash(hash)
+ create!(extract_info(hash))
+ end
+
+ def self.find_by_auth_hash(hash)
+ conditions = extract_info(hash).slice(:provider, :uid)
+ where(conditions).first
+ end
+
+ def self.find(nickname)
+ where(:nickname => nickname).first!
+ end
+
+ def to_param
+ nickname
+ end
+
+ private
+ def self.extract_info(hash)
+ provider = hash.fetch('provider')
+ uid = hash.fetch('uid')
+ nickname = hash.fetch('info',{}).fetch('nickname')
+ email = hash.fetch('info',{}).fetch('email', nil)
+
+ { :provider => provider, :uid => uid, :nickname => nickname, :email => email }
+ end
+end
View
8 app/views/application/_user_actions.html.erb
@@ -0,0 +1,8 @@
+<ul class="nav pull-right">
+ <% if logged_in? %>
+ <li><%= link_to "Logout", logout_path() %></li>
+ <li><%= link_to "Dashboard", dashboard_path() %></li>
+ <% else %>
+ <li><%= link_to "Login with Github", login_path() %></li>
+ <% end %>
+</ul>
View
1  app/views/dashboards/show.html.erb
@@ -0,0 +1 @@
+Welcome back <%= user.nickname %>
View
20 app/views/layouts/application.html.erb
@@ -35,14 +35,24 @@
<%= favicon_link_tag 'images/favicon.ico', :rel => 'shortcut icon' %>
</head>
<body>
+ <nav>
+ <div class="navbar navbar-static-top navbar-inverse">
+ <div class="navbar-inner">
+ <%= link_to "24 Pull Requests", root_path(), :class => 'brand' %>
+ <%= render :partial => 'user_actions' %>
+ </div>
+ </div>
+ </nav>
+
<a class='forkme' href="https://github.com/andrew/24pullrequests" target='_blank'><img src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub"></a>
+
<div class="container">
- <div class="row">
- <div class="span12">
- <%= yield %>
- </div>
- </div><!--/row-->
+ <div class="row">
+ <div class="span12">
+ <%= yield %>
+ </div>
+ </div><!--/row-->
<footer>
<p>Some footery related things down here!</p>
View
1  app/views/users/show.html.erb
@@ -0,0 +1 @@
+Viewing <%= user.nickname %>'s public profile
View
6 config/initializers/omniauth.rb
@@ -0,0 +1,6 @@
+Rails.application.config.middleware.use OmniAuth::Builder do
+ provider :developer, :fields => [:nickname], :uid_field => :nickname unless Rails.env.production?
+ provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
+end
+
+Rails.application.config.default_provider = Rails.env.production? ? :github : :developer
View
11 config/routes.rb
@@ -1,3 +1,14 @@
Tfpullrequests::Application.routes.draw do
+ resources :users
+ resource :dashboard # Singular, only applies to current user
+
+ match '/login', to: 'sessions#new', as: 'login'
+ match '/logout', to: 'sessions#destroy', as: 'logout'
+
+ match '/auth/:provider/callback', to: 'sessions#create'
+ match '/auth/failure', to: 'sessions#failure'
+
root :to => 'static#homepage'
+
+ match '/:id', to: 'users#show' # User public vanity url, must be lowest priority
end
View
16 db/migrate/20121115154537_create_users.rb
@@ -0,0 +1,16 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ create_table :users do |t|
+ t.string :uid, :null => false
+ t.string :provider, :null => false
+ t.string :nickname, :null => false
+
+ t.string :email
+
+ t.timestamps
+ end
+
+ add_index :users, :nickname, :uniq => true
+ add_index :users, [:uid, :provider], :uniq => true
+ end
+end
View
28 db/schema.rb
@@ -0,0 +1,28 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20121115154537) do
+
+ create_table "users", :force => true do |t|
+ t.string "uid", :null => false
+ t.string "provider", :null => false
+ t.string "nickname", :null => false
+ t.string "email"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "users", ["nickname"], :name => "index_users_on_nickname"
+ add_index "users", ["uid", "provider"], :name => "index_users_on_uid_and_provider"
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.