-
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
158 additions
and
2 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
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,20 @@ | ||
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 | ||
redirect_to user | ||
else | ||
flash.now[:danger] = 'Invalid email/password combination' | ||
render 'new' | ||
end | ||
end | ||
|
||
def destroy | ||
log_out | ||
redirect_to root_path | ||
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,18 @@ | ||
module SessionsHelper | ||
def log_in(user) | ||
session[:user_id] = user.id | ||
end | ||
|
||
def log_out | ||
session.delete(:user_id) | ||
@current_user = nil | ||
end | ||
|
||
def current_user | ||
@current_user ||= User.find_by(id: session[:user_id]) | ||
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,20 @@ | ||
<% 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.submit "Log in", class: "btn btn-primary" %> | ||
<% end %> | ||
<p> </p> | ||
<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,9 @@ | ||
require 'test_helper' | ||
|
||
class SessionsControllerTest < ActionDispatch::IntegrationTest | ||
test "should get new" do | ||
get sessions_new_url | ||
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 @@ | ||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
michael: | ||
name: Michael Example | ||
email: michael@example.com | ||
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,40 @@ | ||
require 'test_helper' | ||
|
||
class UsersLoginTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
def setup | ||
@user = users(:michael) | ||
end | ||
|
||
test "login with invalid information" do | ||
get login_path | ||
assert_template 'sessions/new' | ||
post login_path, params: { session: { email: "", password: "" } } | ||
assert_template 'sessions/new' | ||
assert_not flash.empty? | ||
get root_path | ||
assert flash.empty? | ||
end | ||
|
||
test "login with valid information by logout" do | ||
get login_path | ||
post login_path, params: { 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_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 | ||
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