Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'sign-in-out'

  • Loading branch information...
commit 16dc83275d49124d83fab807c31b8fe506d645c9 2 parents b7d3041 + 0dde5f8
@nathanfriemel authored
View
1  app/assets/javascripts/application.js
@@ -12,4 +12,5 @@
//
//= require jquery
//= require jquery_ujs
+//= require bootstrap
//= require_tree .
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/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
1  app/controllers/application_controller.rb
@@ -1,3 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ include SessionsHelper
end
View
20 app/controllers/sessions_controller.rb
@@ -0,0 +1,20 @@
+class SessionsController < ApplicationController
+ def new
+ end
+
+ def create
+ user = User.find_by_email(params[:session][:email])
+ if user && user.authenticate(params[:session][:password])
+ sign_in user
+ redirect_to user
+ else
+ flash.now[:error] = 'Invalid email/password combination'
+ render 'new'
+ end
+ end
+
+ def destroy
+ sign_out
+ redirect_to root_path
+ end
+end
View
1  app/controllers/users_controller.rb
@@ -10,6 +10,7 @@ def new
def create
@user = User.new(params[:user])
if @user.save
+ sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
View
30 app/helpers/sessions_helper.rb
@@ -0,0 +1,30 @@
+module SessionsHelper
+ def sign_in(user)
+ cookies.permanent[:remember_token] = user.remember_token
+ current_user = user
+ end
+
+ def signed_in?
+ !current_user.nil?
+ end
+
+ def current_user=(user)
+ @current_user = user
+ end
+
+ def current_user
+ @current_user ||= user_from_remember_token
+ end
+
+ def sign_out
+ current_user = nil
+ cookies.delete(:remember_token)
+ end
+
+ private
+
+ def user_from_remember_token
+ remember_token = cookies[:remember_token]
+ User.find_by_remember_token(remember_token) unless remember_token.nil?
+ end
+end
View
7 app/models/user.rb
@@ -14,10 +14,17 @@ class User < ActiveRecord::Base
has_secure_password
before_save { |user| user.email = email.downcase }
+ before_save :create_remember_token
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
+
+ private
+
+ def create_remember_token
+ self.remember_token = SecureRandom.urlsafe_base64
+ end
end
View
23 app/views/layouts/_header.html.erb
@@ -4,9 +4,26 @@
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
- <li><%= link_to "Home", root_path %></li>
- <li><%= link_to "Help", help_path %></li>
- <li><%= link_to "Sign in", '#' %></li>
+ <li><%= link_to "Home", root_path %></li>
+ <li><%= link_to "Help", help_path %></li>
+ <% if signed_in? %>
+ <li><%= link_to "Users", '#' %></li>
+ <li id="fat-menu" class="dropdown">
+ <a href="#" class="dropdown-toggle" data-toggle="dropdown">
+ Account <b class="caret"></b>
+ </a>
+ <ul class="dropdown-menu">
+ <li><%= link_to "Profile", current_user %></li>
+ <li><%= link_to "Settings", '#' %></li>
+ <li class="divider"></li>
+ <li>
+ <%= link_to "Sign out", signout_path, method: "delete" %>
+ </li>
+ </ul>
+ </li>
+ <% else %>
+ <li><%= link_to "Sign in", signin_path %></li>
+ <% end %>
</ul>
</nav>
</div>
View
19 app/views/sessions/new.html.erb
@@ -0,0 +1,19 @@
+<% provide(:title, "Sign in") %>
+<h1>Sign in</h1>
+
+<div class="row">
+ <div class="span6 offset3">
+ <%= form_for(:session, url: sessions_path) do |f| %>
+
+ <%= f.label :email %>
+ <%= f.text_field :email %>
+
+ <%= f.label :password %>
+ <%= f.password_field :password %>
+
+ <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
+ <% end %>
+
+ <p>New user? <%= link_to "Sign up now!", signup_path %></p>
+ </div>
+</div>
View
3  config/routes.rb
@@ -1,9 +1,12 @@
SampleApp::Application.routes.draw do
resources :users
+ resources :sessions, only: [:new, :create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
+ match '/signin', to: 'sessions#new'
+ match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
View
6 db/migrate/20120430022433_add_remember_token_to_users.rb
@@ -0,0 +1,6 @@
+class AddRememberTokenToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :remember_token, :string
+ add_index :users, :remember_token
+ end
+end
View
4 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120427031455) do
+ActiveRecord::Schema.define(:version => 20120430022433) do
create_table "users", :force => true do |t|
t.string "name"
@@ -19,8 +19,10 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
+ t.string "remember_token"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
+ add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
end
View
7 spec/models/user_spec.rb
@@ -21,6 +21,8 @@
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
+ it { should respond_to(:remember_token) }
+ it { should respond_to(:authenticate) }
it { should be_valid }
@@ -104,4 +106,9 @@
specify { user_for_invalid_password.should be_false }
end
end
+
+ describe "remember token" do
+ before { @user.save }
+ its(:remember_token) { should_not be_blank }
+ end
end
View
48 spec/requests/authenticaton_pages_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe "Authentication" do
+
+ subject { page }
+
+ describe "signin page" do
+ before { visit signin_path }
+
+ it { should have_selector('h1', text: 'Sign in') }
+ it { should have_selector('title', text: 'Sign in') }
+ end
+
+ describe "signin" do
+ before { visit signin_path }
+
+ describe "with invalid information" do
+ before { click_button "Sign in" }
+
+ it { should have_selector('title', text: 'Sign in') }
+ it { should have_selector('div.alert.alert-error', text: 'Invalid') }
+
+ describe "after visiting another page" do
+ before { click_link "Home" }
+ it { should_not have_selector('div.alert.alert-error') }
+ end
+ end
+
+ describe "with valid information" do
+ let(:user) { FactoryGirl.create(:user) }
+ before do
+ fill_in "Email", with: user.email
+ fill_in "Password", with: user.password
+ click_button "Sign in"
+ end
+
+ it { should have_selector('title', text: user.name) }
+ it { should have_link('Profile', href: user_path(user)) }
+ it { should have_link('Sign out', href: signout_path) }
+ it { should_not have_link('Sign in', href: signin_path) }
+
+ describe "followed by signout" do
+ before { click_link "Sign out" }
+ it { should have_link('Sign in') }
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.