-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ | |
// | ||
//= require jquery | ||
//= require jquery_ujs | ||
//= require bootstrap | ||
//= require_tree . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class ApplicationController < ActionController::Base | ||
protect_from_forgery | ||
include SessionsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class SessionsController < ApplicationController | ||
def new | ||
end | ||
|
||
def create | ||
user = User.find_by_email(params[:session][:email].downcase) | ||
if user && user.authenticate(params[:session][:password]) | ||
# Sign the user in and redirect to the user's show page | ||
sign_in user | ||
redirect_to user | ||
else | ||
flash.now[:error] = 'Invalid email/password combination' # not quite right! | ||
render 'new' | ||
end | ||
end | ||
|
||
def destroy | ||
sign_out | ||
redirect_to root_url | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module SessionsHelper | ||
def sign_in(user) | ||
cookies.permanent[:remember_token] = user.remember_token | ||
self.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.find_by_remember_token(cookies[:remember_token]) | ||
end | ||
|
||
def sign_out | ||
self.current_user = nil | ||
cookies.delete(:remember_token) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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.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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddRememberTokenToUsers < ActiveRecord::Migration | ||
def change | ||
add_column :users, :remember_token, :string | ||
add_index :users, :remember_token | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'spec_helper' | ||
|
||
describe "AuthenticationPages" 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters