Permalink
Browse files

Finish sign in

  • Loading branch information...
1 parent f6c43e9 commit 3cad611725458c56e5f54d163a3864fe696e7a83 Jason Kim committed Mar 15, 2012
View
@@ -24,6 +24,8 @@ gem 'jquery-rails', '2.0.0'
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '1.4.0'
+ gem 'cucumber-rails', '1.2.1'
+ gem 'database_cleaner', '0.7.0'
end
group :production do
View
@@ -49,6 +49,17 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.2.0)
+ cucumber (1.1.9)
+ builder (>= 2.1.2)
+ diff-lcs (>= 1.1.2)
+ gherkin (~> 2.9.0)
+ json (>= 1.4.6)
+ term-ansicolor (>= 1.0.6)
+ cucumber-rails (1.2.1)
+ capybara (>= 1.1.2)
+ cucumber (>= 1.1.3)
+ nokogiri (>= 1.5.0)
+ database_cleaner (0.7.0)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.3.0)
@@ -59,14 +70,16 @@ GEM
factory_girl (~> 2.3.0)
railties (>= 3.0.0)
ffi (1.0.11)
+ gherkin (2.9.0)
+ json (>= 1.4.6)
hike (1.2.1)
i18n (0.6.0)
journey (1.0.3)
jquery-rails (2.0.0)
railties (>= 3.2.0.beta, < 5.0)
thor (~> 0.14)
json (1.6.5)
- mail (2.4.3)
+ mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
@@ -129,6 +142,7 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.5)
+ term-ansicolor (1.0.7)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
@@ -150,6 +164,8 @@ DEPENDENCIES
bootstrap-sass (= 2.0.0)
capybara (= 1.1.2)
coffee-rails (= 3.2.2)
+ cucumber-rails (= 1.2.1)
+ database_cleaner (= 0.7.0)
factory_girl_rails (= 1.4.0)
jquery-rails (= 2.0.0)
pg (= 0.12.2)
@@ -12,4 +12,5 @@
//
//= require jquery
//= require jquery_ujs
-//= require_tree .
+//= require bootstrap
+//= require_tree .
@@ -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/
@@ -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/
@@ -1,3 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ include SessionsHelper
end
@@ -0,0 +1,21 @@
+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
@@ -2,6 +2,7 @@ class UsersController < ApplicationController
def create
@user = User.new(params[:user])
if @user.save
+ sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
@@ -16,4 +17,6 @@ def show
def new
@user = User.new
end
+
+
end
@@ -0,0 +1,33 @@
+module SessionsHelper
+
+ def sign_in(user)
+ cookies.permanent[:remember_token] = user.remember_token
+ current_user = user
+ end
+
+ def current_user=(user)
+ @current_user = user
+ end
+
+ def current_user
+ @current_user ||= user_from_remember_token
+ end
+
+ def signed_in?
+ !current_user.nil?
+ 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
@@ -12,6 +12,7 @@
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
+ before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
@@ -20,4 +21,10 @@ class User < ActiveRecord::Base
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
@@ -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>
@@ -0,0 +1,18 @@
+<% 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
@@ -0,0 +1,8 @@
+<%
+rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
+rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
+std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
+%>
+default: <%= std_opts %> features
+wip: --tags @wip:3 --wip features
+rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
View
@@ -12,7 +12,7 @@ development:
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
-test:
+test: &test
adapter: sqlite3
database: db/test.sqlite3
pool: 5
@@ -23,3 +23,6 @@ production:
database: db/production.sqlite3
pool: 5
timeout: 5000
+
+cucumber:
+ <<: *test
View
@@ -1,10 +1,13 @@
SampleApp::Application.routes.draw do
resources :users
+ resources :sessions, only: [:new, :create, :destroy]
+
+ match '/signup', to: 'users#new'
+ match '/signin', to: 'sessions#new'
+ match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
# match '/', to: 'static_pages#home'
-
- match '/signup', to: 'users#new'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
@@ -0,0 +1,6 @@
+class AddRememberTokenToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :remember_token, :string
+ add_index :users, :remember_token
+ end
+end
View
@@ -11,16 +11,18 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120313003545) do
+ActiveRecord::Schema.define(:version => 20120315044928) do
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
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
@@ -0,0 +1,56 @@
+# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
+# It is recommended to regenerate this file in the future when you upgrade to a
+# newer version of cucumber-rails. Consider adding your own code to a new file
+# instead of editing this one. Cucumber will automatically load all features/**/*.rb
+# files.
+
+require 'cucumber/rails'
+
+# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
+# order to ease the transition to Capybara we set the default here. If you'd
+# prefer to use XPath just remove this line and adjust any selectors in your
+# steps to use the XPath syntax.
+Capybara.default_selector = :css
+
+# By default, any exception happening in your Rails application will bubble up
+# to Cucumber so that your scenario will fail. This is a different from how
+# your application behaves in the production environment, where an error page will
+# be rendered instead.
+#
+# Sometimes we want to override this default behaviour and allow Rails to rescue
+# exceptions and display an error page (just like when the app is running in production).
+# Typical scenarios where you want to do this is when you test your error pages.
+# There are two ways to allow Rails to rescue exceptions:
+#
+# 1) Tag your scenario (or feature) with @allow-rescue
+#
+# 2) Set the value below to true. Beware that doing this globally is not
+# recommended as it will mask a lot of errors for you!
+#
+ActionController::Base.allow_rescue = false
+
+# Remove/comment out the lines below if your app doesn't have a database.
+# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
+begin
+ DatabaseCleaner.strategy = :transaction
+rescue NameError
+ raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
+end
+
+# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
+# See the DatabaseCleaner documentation for details. Example:
+#
+# Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
+# DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
+# end
+#
+# Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
+# DatabaseCleaner.strategy = :transaction
+# end
+#
+
+# Possible values are :truncation and :transaction
+# The :transaction strategy is faster, but might give you threading problems.
+# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature
+Cucumber::Rails::Database.javascript_strategy = :truncation
+
Oops, something went wrong.

0 comments on commit 3cad611

Please sign in to comment.