Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

I2: Authenticating Users

  • Loading branch information...
commit 002af34c83dcc13946f7378a6a8e288376523e51 1 parent 1719be4
@aaronmix authored
View
3  app/assets/javascripts/admin.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/
View
3  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/
View
3  app/assets/stylesheets/admin.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Admin controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
3  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/
View
5 app/controllers/admin_controller.rb
@@ -0,0 +1,5 @@
+class AdminController < ApplicationController
+ def index
+ @total_orders = Order.count
+ end
+end
View
19 app/controllers/sessions_controller.rb
@@ -0,0 +1,19 @@
+class SessionsController < ApplicationController
+ def new
+ end
+
+ def create
+ user = User.find_by_name(params[:name])
+ if user and user.authenticate(params[:password])
+ session[:user_id] = user.id
+ redirect_to admin_url
+ else
+ redirect_to login_url, alert: "Invalid user/password combination"
+ end
+ end
+
+ def destroy
+ session[:user_id] = nil
+ redirect_to store_url, notice: "Logged out"
+ end
+end
View
2  app/helpers/admin_helper.rb
@@ -0,0 +1,2 @@
+module AdminHelper
+end
View
2  app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
View
4 app/views/admin/index.html.erb
@@ -0,0 +1,4 @@
+<h1>Welcome</h1>
+
+It's <%= Time.now %>
+We have <%= pluralize(@total_orders, "order")%>
View
2  app/views/sessions/create.html.erb
@@ -0,0 +1,2 @@
+<h1>Sessions#create</h1>
+<p>Find me in app/views/sessions/create.html.erb</p>
View
2  app/views/sessions/destroy.html.erb
@@ -0,0 +1,2 @@
+<h1>Sessions#destroy</h1>
+<p>Find me in app/views/sessions/destroy.html.erb</p>
View
25 app/views/sessions/new.html.erb
@@ -0,0 +1,25 @@
+<div class="depot_form">
+ <% if flash[:alert] %>
+ <p id="notice"><%= flash[:alert] %></p>
+ <% end %>
+
+ <%= form_tag do %>
+ <fieldset>
+ <legend>Please Log In</legend>
+
+ <div>
+ <%= label_tag :name, 'Name:' %>
+ <%= text_field_tag :name, params[:name]%>
+ </div>
+
+ <div>
+ <%= label_tag :password, 'Password'%>
+ <%= password_field_tag :password, params[:password]%>
+ </div>
+
+ <div>
+ <%= submit_tag "Login"%>
+ </div>
+ </fieldset>
+ <% end %>
+</div>
View
14 config/routes.rb
@@ -7,6 +7,20 @@
# Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
#---
Depot::Application.routes.draw do
+ get "admin" => 'admin #index'
+
+ controller :sessions do
+ get 'login' => :new
+ post 'login' => :create
+ delete 'logout' => :destroy
+ end
+
+ # get "sessions/new"
+ #
+ # get "sessions/create"
+ #
+ # get "sessions/destroy"
+
resources :users
resources :orders
View
4 test/fixtures/users.yml
@@ -1,8 +1,8 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
- name: MyString
- password_digest: MyString
+ name: dave
+ password_digest: <%= BCrypt::Password.create('secret') %>
two:
name: MyString
View
9 test/functional/admin_controller_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class AdminControllerTest < ActionController::TestCase
+ test "should get index" do
+ get :index
+ assert_response :success
+ end
+
+end
View
26 test/functional/sessions_controller_test.rb
@@ -0,0 +1,26 @@
+require 'test_helper'
+
+class SessionsControllerTest < ActionController::TestCase
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should login" do
+ dave = users(:one)
+ post :create, name: dave.name, password: 'secret'
+ assert_redirected_to admin_url
+ assert_equal dave.id, session[:user_id]
+ end
+
+ test "should fail login" do
+ dave = users(:one)
+ post :create, name: dave.name, password: 'secret'
+ assert_redirected_to login_url
+ end
+
+ test "should logout" do
+ delete :destroy
+ assert_redirected_to store_url
+
+end
View
4 test/unit/helpers/admin_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class AdminHelperTest < ActionView::TestCase
+end
View
4 test/unit/helpers/sessions_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class SessionsHelperTest < ActionView::TestCase
+end
Please sign in to comment.
Something went wrong with that request. Please try again.