Commit cb5fb361 by Ba Toi Dang

Revert "Merge branch 'basic-login' into 'master'"

This reverts merge request !7
parent 600940ba
......@@ -65,5 +65,4 @@ end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'bcrypt', '~> 3.1', '>= 3.1.12'
gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
\ No newline at end of file
gem 'bcrypt', '~> 3.1', '>= 3.1.12'
\ No newline at end of file
......@@ -90,10 +90,6 @@ GEM
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jquery-rails (4.3.3)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
......@@ -215,7 +211,6 @@ DEPENDENCIES
chromedriver-helper
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
jquery-rails (~> 4.3, >= 4.3.3)
listen (>= 3.0.5, < 3.2)
pg (~> 1.0)
puma (~> 3.11)
......
//= require jquery
//= require bootstrap
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require activestorage
//= require turbolinks
......
/*
= require_self
= require 'custom'
*/
\ No newline at end of file
@import 'custom';
\ No newline at end of file
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
def hello
render html: "hello, world!"
end
end
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_url
end
end
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
# debugger
end
def new
......@@ -10,7 +11,6 @@ class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
......
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
def logged_in?
!current_user.nil?
end
def log_out
session.delete(:user_id)
@current_user = nil
end
end
......@@ -8,10 +8,4 @@ class User < ApplicationRecord
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end
......@@ -5,24 +5,7 @@
<ul class="nav navbar-nav navbar-right">
<li><%= link_to 'Home', root_path %></li>
<li><%= link_to 'Help', help_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", '#' %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", "#" %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: :delete %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to 'Log in', login_path %></li>
<% end %>
<li><%= link_to 'Log in', '#' %></li>
</ul>
</nav>
</div>
......
<% 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>New user? <%= link_to 'Sign up now!', signup_path %></p>
</div>
</div>
\ No newline at end of file
......@@ -3,7 +3,7 @@
<div class="row">
<div class="col-md-6 col-md-offet-3">
<%= form_for @user do |f| %>
<%= form_for @user, url: signup_path do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
......
......@@ -3,13 +3,7 @@ Rails.application.routes.draw do
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
require 'test_helper'
class SessionsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get login_path
assert_response :success
end
end
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
\ No newline at end of file
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
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 followed 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_url
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
......@@ -10,6 +10,8 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
password_confirmation: "bar" } }
end
assert_template 'users/new'
assert_select 'div#<CSS id for error explanation>'
assert_select 'div.<CSS class for field with error>'
end
test "valid signup information" do
......@@ -22,6 +24,5 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
end
follow_redirect!
assert_template 'users/show'
assert is_logged_in?
end
end
......@@ -5,10 +5,5 @@ require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
#return true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Add more helper methods to be used by all tests here...
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment