-
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
1 parent
bf76f9f
commit 3e8a7ca
Showing
21 changed files
with
277 additions
and
8 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,5 +12,6 @@ | |
// | ||
//= require jquery | ||
//= require jquery_ujs | ||
//= require bootstrap | ||
//= require turbolinks | ||
//= 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://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 password or username" | ||
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,39 @@ | ||
module SessionsHelper | ||
def log_in(user) | ||
session[:user_id] = user.id | ||
end | ||
|
||
def remember(user) | ||
user.remember | ||
cookies.permanent.signed[:user_id] = user.id | ||
cookies.permanent[:remember_token] = user.remember_token | ||
end | ||
|
||
def forget(user) | ||
user.forget | ||
cookies.delete(:user_id) | ||
cookies.delete(:remember_token) | ||
end | ||
|
||
def log_out | ||
forget(current_user) | ||
session.delete(:user_id) | ||
@current_user = nil | ||
end | ||
|
||
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 | ||
|
||
def logged_in? | ||
!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
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.email_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 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,5 @@ | ||
#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,20 @@ | ||
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,49 @@ | ||
require 'test_helper' | ||
|
||
class UsersLoginTest < ActionDispatch::IntegrationTest | ||
|
||
def setup | ||
@user = users(:michael) | ||
end | ||
|
||
test "login with valid information and 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 | ||
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 invalid information" do | ||
get login_path | ||
assert_template 'sessions/new' | ||
post login_path, session: { email: "", password: "" } | ||
assert_template 'sessions/new' | ||
assert_not flash.empty? | ||
get root_path | ||
assert flash.empty? | ||
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