-
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
22 changed files
with
284 additions
and
18 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
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://coffeescript.org/ |
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,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
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]) | ||
log_in user | ||
params[:session][:remember_me] == '1' ? remember(user) : forget(user) | ||
redirect_to user | ||
else | ||
flash.now[:danger] = 'Invalid email/password combination' # Not quite right! | ||
render 'new' | ||
end | ||
end | ||
|
||
def destroy | ||
log_out if logged_in? | ||
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,48 @@ | ||
module SessionsHelper | ||
|
||
# Logs in the given user. | ||
def log_in(user) | ||
session[:user_id] = user.id | ||
end | ||
|
||
# Remembers a user in a persistent session. | ||
def remember(user) | ||
user.remember | ||
cookies.permanent.signed[:user_id] = user.id | ||
cookies.permanent[:remember_token] = user.remember_token | ||
end | ||
|
||
# Returns the user corresponding to the remember token cookie. | ||
def current_user | ||
if (user_id = session[:user_id]) | ||
@current_user ||= User.find_by(id: user_id) | ||
elsif (user_id = cookies.signed[:user_id]) | ||
user = User.find_by(id: user_id) | ||
if user && user.authenticated?(cookies[:remember_token]) | ||
log_in user | ||
@current_user = user | ||
end | ||
end | ||
end | ||
|
||
# Returns true if the user is logged in, false otherwise. | ||
def logged_in? | ||
!current_user.nil? | ||
end | ||
|
||
# Forgets a persistent session. | ||
def forget(user) | ||
user.forget | ||
cookies.delete(:user_id) | ||
cookies.delete(:remember_token) | ||
end | ||
|
||
# Logs out the current user. | ||
def log_out | ||
forget(current_user) | ||
session.delete(:user_id) | ||
@current_user = nil | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,40 @@ | ||
class User < ActiveRecord::Base | ||
attr_accessor :remember_token | ||
before_save { self.email = email.downcase } | ||
validates :name, presence: true, length: { maximum: 50 } | ||
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | ||
validates :email, presence: true, length: { maximum: 255 }, | ||
format: { with: VALID_EMAIL_REGEX }, | ||
format: { with: VALID_EMAIL_REGEX }, | ||
uniqueness: { case_sensitive: false } | ||
has_secure_password | ||
validates :password, length: { minimum: 6 } | ||
|
||
# Returns the hash digest of the given string. | ||
def User.digest(string) | ||
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : | ||
BCrypt::Engine.cost | ||
BCrypt::Password.create(string, cost: cost) | ||
end | ||
|
||
# Returns a random token. | ||
def User.new_token | ||
SecureRandom.urlsafe_base64 | ||
end | ||
|
||
# Remembers a user in the database for use in persistent sessions. | ||
def remember | ||
self.remember_token = User.new_token | ||
update_attribute(:remember_digest, User.digest(remember_token)) | ||
end | ||
|
||
# Returns true if the given token matches the digest. | ||
def authenticated?(remember_token) | ||
return false if remember_digest.nil? | ||
BCrypt::Password.new(remember_digest).is_password?(remember_token) | ||
end | ||
|
||
# Forgets a user. | ||
def forget | ||
update_attribute(:remember_digest, nil) | ||
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,24 @@ | ||
<% provide(:title, "Log in") %> | ||
<h1>Log in</h1> | ||
|
||
<div class="row"> | ||
<div class="col-md-6 col-md-offset-3"> | ||
<%= form_for(:session, url: login_path) do |f| %> | ||
|
||
<%= f.label :email %> | ||
<%= f.text_field :email, class: 'form-control' %> | ||
|
||
<%= f.label :password %> | ||
<%= f.password_field :password, class: 'form-control' %> | ||
|
||
<%= f.label :remember_me, class: "checkbox inline" do %> | ||
<%= f.check_box :remember_me %> | ||
<span>Remember me on this computer</span> | ||
<% end %> | ||
|
||
<%= f.submit "Log in", class: "btn 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,5 @@ | ||
class AddRememberDigestToUsers < ActiveRecord::Migration | ||
def change | ||
add_column :users, :remember_digest, :string | ||
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 was deleted.
Oops, something went wrong.
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,9 @@ | ||
require 'test_helper' | ||
|
||
class SessionsControllerTest < ActionController::TestCase | ||
test "should get new" do | ||
get :new | ||
assert_response :success | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
# empty | ||
michael: | ||
name: Michael Example | ||
email: [email protected] | ||
password_digest: <%= User.digest('password') %> |
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,19 @@ | ||
require 'test_helper' | ||
|
||
class SessionsHelperTest < ActionView::TestCase | ||
|
||
def setup | ||
@user = users(:michael) | ||
remember(@user) | ||
end | ||
|
||
test "current_user returns right user when session is nil" do | ||
assert_equal @user, current_user | ||
assert is_logged_in? | ||
end | ||
|
||
test "current_user returns nil when remember digest is wrong" do | ||
@user.update_attribute(:remember_digest, User.digest(User.new_token)) | ||
assert_nil current_user | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'test_helper' | ||
|
||
class UsersLoginTest < ActionDispatch::IntegrationTest | ||
|
||
def setup | ||
@user = users(:michael) | ||
end | ||
|
||
test "login with valid information followed by logout" do | ||
get login_path | ||
post login_path, session: { email: @user.email, password: 'password' } | ||
assert is_logged_in? | ||
assert_redirected_to @user | ||
follow_redirect! | ||
assert_template 'users/show' | ||
assert_select "a[href=?]", login_path, count: 0 | ||
assert_select "a[href=?]", logout_path | ||
assert_select "a[href=?]", user_path(@user) | ||
delete logout_path | ||
assert_not is_logged_in? | ||
assert_redirected_to root_url | ||
# Simulate a user clicking logout in a second window. | ||
delete logout_path | ||
follow_redirect! | ||
assert_select "a[href=?]", login_path | ||
assert_select "a[href=?]", logout_path, count: 0 | ||
assert_select "a[href=?]", user_path(@user), count: 0 | ||
end | ||
|
||
test "login with remembering" do | ||
log_in_as(@user, remember_me: '1') | ||
assert_not_nil cookies['remember_token'] | ||
end | ||
|
||
test "login without remembering" do | ||
log_in_as(@user, remember_me: '0') | ||
assert_nil cookies['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