Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

login view

  • Loading branch information...
commit e97bdba194430e11959373df401d074cde73b286 1 parent 67519a4
lukasz authored
View
2  app/controllers/application_controller.rb
@@ -1,5 +1,7 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ include SessionsHelper
+
before_filter :get_categories
def get_categories
View
8 app/controllers/sessions_controller.rb
@@ -4,6 +4,14 @@ 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 or password"
+ render 'new'
+ end
end
def destroy
View
6 app/helpers/sessions_helper.rb
@@ -1,2 +1,8 @@
module SessionsHelper
+
+ def sign_in(user)
+ cookies.permanent[:remember_token] = user.remember_token
+ current_user = user
+ 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: 20 }
EMAIL_REGEXP = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: EMAIL_REGEXP }, 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
2  app/views/layouts/_header.html.erb
@@ -8,7 +8,7 @@
<li><%= link_to "Home", root_url %></li>
<li><%= link_to "About", home_about_path %></li>
<li><%= link_to "Help", home_help_path %></li>
- <li><%= link_to "Sign in", "#" %></li>
+ <li><%= link_to "Sign in", signin_path %></li>
</ul>
</nav>
</div>
View
18 app/views/sessions/new.html.erb
@@ -1,2 +1,20 @@
<% 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.text_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
6 db/migrate/20120504225640_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 => 20120504073715) do
+ActiveRecord::Schema.define(:version => 20120504225640) do
create_table "categories", :force => true do |t|
t.string "name"
@@ -35,8 +35,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
6 spec/models/user_spec.rb
@@ -21,6 +21,7 @@
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
+ it { should reposnd_to(:remember_token) }
it { should be_valid }
@@ -39,6 +40,11 @@
it { should_not be_valid }
end
+ describt "remember token" do
+ before { @user.save }
+ its(:remember_token) { should_not be_blank }
+ end
+
describe "when email format is invalid" do
it "should be invalid" do
addresses = ["lukasz@ex,pre", "result_lk.go", "somth@gmlr."]
View
35 spec/requests/authentication_pages_spec.rb
@@ -7,7 +7,38 @@
describe "signin page" do
before { visit signin_path }
- it { should have_selector('h1', text: 'Sign in')}
- it { should have_selector('title', text: 'Sign in')}
+ 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 { visit root_url }
+
+ 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"
+
+ 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) }
+ end
+ end
end
end
View
13 spec/requests/authentication_pages_spec_flymake.rb
@@ -1,13 +0,0 @@
-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
-end
Please sign in to comment.
Something went wrong with that request. Please try again.