Skip to content

Commit fdde1cc

Browse files
author
Ryan Bigg
committed
Section 6.3.4: Implemented signing in
1 parent 01aaacd commit fdde1cc

File tree

20 files changed

+156
-1
lines changed

20 files changed

+156
-1
lines changed

ticketee/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ gem 'simple_form', '3.1.0.rc2'
2929

3030
# Use ActiveModel has_secure_password
3131
gem 'bcrypt', '~> 3.1.9'
32+
gem 'warden', '~> 1.2.3'
3233

3334
# Use Unicorn as the app server
3435
# gem 'unicorn'

ticketee/Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ GEM
175175
uglifier (2.5.3)
176176
execjs (>= 0.3.0)
177177
json (>= 1.8.0)
178+
warden (1.2.3)
179+
rack (>= 1.0)
178180
web-console (2.0.0.beta4)
179181
activemodel (~> 4.0)
180182
binding_of_caller (= 0.7.3.pre1)
@@ -205,4 +207,5 @@ DEPENDENCIES
205207
sqlite3
206208
turbolinks
207209
uglifier (>= 1.3.0)
210+
warden (~> 1.2.3)
208211
web-console (~> 2.0.0.beta4)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/

ticketee/app/assets/stylesheets/application.css.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ a.delete {
4747
.alert-alert {
4848
@extend .alert-danger;
4949
}
50+
51+
.form {
52+
input[type=text], input[type=password] {
53+
@extend .form-control;
54+
}
55+
56+
input[type=submit] {
57+
@extend .btn;
58+
@extend .btn-primary;
59+
}
60+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the sessions controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

ticketee/app/controllers/application_controller.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,16 @@ class ApplicationController < ActionController::Base
22
# Prevent CSRF attacks by raising an exception.
33
# For APIs, you may want to use :null_session instead.
44
protect_from_forgery with: :exception
5+
6+
private
7+
8+
def current_user
9+
@current_user ||= env['warden'].user
10+
end
11+
helper_method :current_user
12+
13+
def signed_in?
14+
env["warden"].authenticated?
15+
end
16+
helper_method :signed_in?
517
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class SessionsController < ApplicationController
2+
def create
3+
if env['warden'].authenticate
4+
flash[:notice] = "Signed in successfully."
5+
6+
redirect_to root_url
7+
else
8+
flash[:alert] = "Invalid email or password."
9+
render :new
10+
end
11+
end
12+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module SessionsHelper
2+
end

ticketee/app/models/user.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
class User < ActiveRecord::Base
22
has_secure_password
3+
4+
def self.authenticate(email, password)
5+
user = find_by(email: email)
6+
return user if user && user.authenticate(password)
7+
end
38
end

ticketee/app/views/layouts/application.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<li class='<%= 'active' if current_page?('/users/sign_up') %>'>
2828
<%= link_to "Sign up", sign_up_path %>
2929
</li>
30+
<li class='<%= 'active' if current_page?('/users/sign_in') %>'>
31+
<%= link_to "Sign in", sign_in_path %>
32+
</li>
3033
</ul>
3134
</div>
3235
</nav>

0 commit comments

Comments
 (0)