Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add authentication to the app

  • Loading branch information...
commit 951da52ba0db2f34c8d9457d4969fea0c80831fa 1 parent 3b9f4e5
xmonkey authored
4 Gemfile
View
@@ -5,7 +5,7 @@ gem 'bootstrap-sass'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
-
+gem 'bcrypt-ruby'
group :production do
gem 'pg'
@@ -13,7 +13,7 @@ end
group :development do
gem 'sqlite3'
- gem "better_errors"
+ #gem "better_errors"
end
# Gems used only for assets and not required
7 Gemfile.lock
View
@@ -29,12 +29,9 @@ GEM
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
- better_errors (0.0.8)
- coderay
- erubis
+ bcrypt-ruby (3.0.1)
bootstrap-sass (2.1.0.0)
builder (3.0.3)
- coderay (1.0.8)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
@@ -109,7 +106,7 @@ PLATFORMS
ruby
DEPENDENCIES
- better_errors
+ bcrypt-ruby
bootstrap-sass
coffee-rails (~> 3.2.1)
jquery-rails
3  app/assets/javascripts/sessions.js.coffee
View
@@ -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  app/assets/javascripts/user.js.coffee
View
@@ -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  app/assets/stylesheets/sessions.css.scss
View
@@ -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/
3  app/assets/stylesheets/user.css.scss
View
@@ -0,0 +1,3 @@
+// Place all the styles related to the User controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
10 app/controllers/application_controller.rb
View
@@ -1,3 +1,13 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ def current_user
+ session[:user]
+ end
+
+ def logged_in?
+ !!current_user
+ end
+
+ helper_method :current_user, :logged_in?
end
20 app/controllers/sessions_controller.rb
View
@@ -0,0 +1,20 @@
+class SessionsController < ApplicationController
+ def new
+ end
+
+ def create
+ user = User.find_by_name(params[:name])
+ if user && user.authenticate(params[:password])
+ session[:user] = user
+ redirect_to root_url, notice: "Logged in as #{user.name}"
+ else
+ flash[:error] = "Wrong user name/password!"
+ render :new
+ end
+ end
+
+ def destroy
+ session[:user] = nil
+ redirect_to root_url, notice: "Logged out!"
+ end
+end
14 app/controllers/users_controller.rb
View
@@ -0,0 +1,14 @@
+class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.create(params[:user])
+ if @user.save
+ redirect_to root_path, :notice => "User #{@user.name} was created!"
+ else
+ render :new
+ end
+ end
+end
2  app/helpers/sessions_helper.rb
View
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
2  app/helpers/user_helper.rb
View
@@ -0,0 +1,2 @@
+module UserHelper
+end
5 app/models/user.rb
View
@@ -0,0 +1,5 @@
+class User < ActiveRecord::Base
+ attr_accessible :name, :password_digest, :password
+ has_secure_password
+ validates :name, :uniqueness => true, :presence => true
+end
9 app/views/layouts/_navigation.html.erb
View
@@ -1 +1,8 @@
-Navigation
+Welcome!
+<% if logged_in? %>
+ <%= current_user.name%>
+ |
+ <%= link_to "Log out", log_out_path %>
+<% else %>
+ <%= link_to "Sign in", sign_in_path %>
+<% end %>
16 app/views/sessions/new.html.erb
View
@@ -0,0 +1,16 @@
+<h1>Sign In</h1>
+<%= form_tag sign_in_path do %>
+ <div class="field">
+ <%= label_tag :name %><br />
+ <%= text_field_tag :name %>
+ </div>
+ <div class="field">
+ <%= label_tag :password %><br />
+ <%= password_field_tag :password %>
+ </div>
+ <div class="actions">
+ <%= submit_tag "Sign in" %>
+ </div>
+Haven't registered yet?
+ <%= link_to "Sign Up now!", sign_up_path %>
+<% end %>
23 app/views/users/new.html.erb
View
@@ -0,0 +1,23 @@
+<h1>Sign Up</h1>
+<% if @user.errors.any? %>
+ <div id ="error_explanation">
+ <ul>
+ <% @user.errors.full_messages.each do |msg| %>
+ <li><%= msg%></li>
+ <% end %>
+ </ul>
+ </div>
+<% end %>
+<%= form_for (@user) do |f| %>
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </div>
+ <div class="field">
+ <%= f.label :password %><br />
+ <%= f.password_field :password %>
+ </div>
+ <div class="actions">
+ <%= f.submit "Sign Up!" %>
+ </div>
+<% end %>
7 config/routes.rb
View
@@ -1,4 +1,11 @@
CourseProject::Application.routes.draw do
+ get 'sign_up', to: "users#new", :as => :sign_up
+ resources :users, :only => :create
+
+ get 'sign_in', to: "sessions#new", :as => :sign_in
+ post 'sign_in', to: "sessions#create"
+ get 'log_out', to: "sessions#destroy", :as => :log_out
+
#root to: "main#index"
root to: "posts#index"
resources :posts, :only => [:index, :show, :new, :create] do
10 db/migrate/20121214031603_create_users.rb
View
@@ -0,0 +1,10 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ create_table :users do |t|
+ t.string :password_digest
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
9 db/schema.rb
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121206142713) do
+ActiveRecord::Schema.define(:version => 20121214031603) do
create_table "comments", :force => true do |t|
t.text "content"
@@ -29,6 +29,13 @@
t.datetime "updated_at", :null => false
end
+ create_table "users", :force => true do |t|
+ t.string "password_digest"
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "votes", :force => true do |t|
t.boolean "upvote"
t.integer "post_id"
9 test/fixtures/users.yml
View
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ password_digest: MyString
+ name: MyString
+
+two:
+ password_digest: MyString
+ name: MyString
7 test/functional/sessions_controller_test.rb
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class SessionsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
9 test/functional/user_controller_test.rb
View
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class UserControllerTest < ActionController::TestCase
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+end
4 test/unit/helpers/sessions_helper_test.rb
View
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class SessionsHelperTest < ActionView::TestCase
+end
4 test/unit/helpers/user_helper_test.rb
View
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class UserHelperTest < ActionView::TestCase
+end
7 test/unit/user_test.rb
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class UserTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.