Commit bf9c39bf by Son Do Hong

Merge branch 'basic-design' into 'master'

Implement basic login

See merge request !7
parents c52b154e 87259c26
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
// //
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives. // about supported directives.
// //= require jquery
//= require bootstrap
//= require rails-ujs //= require rails-ujs
//= require turbolinks //= require turbolinks
//= require_tree . //= require_tree .
# 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/
// 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/
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery with: :exception protect_from_forgery with: :exception
def hello include SessionsHelper
render html: "hello,world!"
end
end end
class SessionsController < ApplicationController
def new
redirect_to @current_user if logged_in?
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
...@@ -11,8 +11,9 @@ class UsersController < ApplicationController ...@@ -11,8 +11,9 @@ class UsersController < ApplicationController
def create def create
@user = User.new(user_params) @user = User.new(user_params)
if @user.save if @user.save
redirect_to @user log_in @user
flash[:success] = "Welcome to the Sample App!" flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else else
render "new" render "new"
end end
......
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Returns the current logged-in user (if any)
def current_user
if session[:user_id]
@current_user ||= User.find_by(id: session[:user_id])
end
end
# Return true if the user is logged in, false otherwise .
def logged_in?
current_user.present?
end
# Logs out the current user
def log_out
session.delete(:user_id)
@current_user = nil
end
end
...@@ -5,7 +5,12 @@ class User < ApplicationRecord ...@@ -5,7 +5,12 @@ class User < ApplicationRecord
validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false } validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 } validates :password, presence: true, length: { minimum: 6 }
before_save { email.downcase! } before_save { email.downcase! } #Call Backs
has_secure_password has_secure_password
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end end
...@@ -5,7 +5,24 @@ ...@@ -5,7 +5,24 @@
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li> <li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li> <li><%= link_to "Help", help_path %></li>
<li><%= link_to "Log in", '#' %></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 %>
</ul> </ul>
</nav> </nav>
</div> </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>
...@@ -10,5 +10,4 @@ ...@@ -10,5 +10,4 @@
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %> <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div> </div>
<%= link_to image_tag("rails.png", alt: "Rails logo"), <%= link_to image_tag("rails.png", alt: "Rails logo"),"http://rubyonrails.org/" %>
'http://rubyonrails.org/' %>
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<div class="row"> <div class="row">
<div class="col-md-6 col-md-offset-3"> <div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: signup_path) do |f| %> <%= form_for(@user) do |f| %>
<%= render "shared/error_messages" %> <%= render "shared/error_messages" %>
<%= f.label :name %> <%= f.label :name %>
<%= f.text_field :name, class: "form-control" %> <%= f.text_field :name, class: "form-control" %>
......
require_relative 'boot' require_relative "boot"
require 'rails/all' require "rails/all"
# Require the gems listed in Gemfile, including any gems # Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production. # you"ve limited to :test, :development, or :production.
Bundler.require(*Rails.groups) Bundler.require(*Rails.groups)
module SampleApp module SampleApp
......
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile. require "bundler/setup" # Set up gems listed in the Gemfile.
Rails.application.routes.draw do Rails.application.routes.draw do
root "static_pages#home" root "static_pages#home"
get "users/new"
get "/help", to: "static_pages#help" get "/help", to: "static_pages#help"
get "/about", to: "static_pages#about" get "/about", to: "static_pages#about"
get "/contact", to: "static_pages#contact" get "/contact", to: "static_pages#contact"
get "/signup", to: "users#new" 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 resources :users
end end
require "test_helper"
class SessionsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get login_path
assert_response :success
end
end
...@@ -2,8 +2,7 @@ require "test_helper" ...@@ -2,8 +2,7 @@ require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest class UsersControllerTest < ActionDispatch::IntegrationTest
test "should get new" do test "should get new" do
get users_new_url get signup_path
assert_response :success assert_response :success
end end
end end
# empty michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest("password") %>
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" do
get login_path
post login_path, params: {session: {email: @user.email,password: "password"}}
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)
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
...@@ -5,7 +5,7 @@ class UsersSignupTest < ActionDispatch::IntegrationTest ...@@ -5,7 +5,7 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
test "invalid signup information" do test "invalid signup information" do
get signup_path get signup_path
assert_no_difference "User.count" do assert_no_difference "User.count" do
post users_path, params: { user: { name: "", email: "user@invalid", password: "foo", password_confirmation: "bar" } } post users_path, params: {user: {name: "", email: "user@invalid", password: "foo", password_confirmation: "bar"}}
end end
assert_template "users/new" assert_template "users/new"
assert_select "div#error_explanation" assert_select "div#error_explanation"
...@@ -15,10 +15,11 @@ class UsersSignupTest < ActionDispatch::IntegrationTest ...@@ -15,10 +15,11 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
test "valid signup information" do test "valid signup information" do
get signup_path get signup_path
assert_difference "User.count", 1 do assert_difference "User.count", 1 do
post users_path, params: { user: { name: "Example User", email: "user@example.com", password: "password", password_confirmation: "password" } } post users_path, params: {user: {name: "Example User", email: "user@example.com", password: "password", password_confirmation: "password"}}
end end
follow_redirect! follow_redirect!
assert_template "users/show" assert_template "users/show"
assert_not flash.empty? assert_not flash.empty?
assert is_logged_in?
end end
end end
...@@ -6,5 +6,9 @@ class ActiveSupport::TestCase ...@@ -6,5 +6,9 @@ class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all fixtures :all
include ApplicationHelper include ApplicationHelper
# Add more helper methods to be used by all tests here...
#Returns true if a test user is logged in.
def is_logged_in?
session[:user_id].present?
end
end 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