Commit e789eda4 by Quang Vinh Nguyen

Implement basic login

parent 2c862ce2
......@@ -9,7 +9,8 @@
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require bootstrap
//= require rails-ujs
//= require turbolinks
//= 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/
# That
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
render html: "hello, world!"
end
include SessionsHelper
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 the user in and redirect to the user's show page.
log_in user
redirect_to user
else
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
# Logs out the current user.
def log_out
session.delete(:user_id)
@current_user = nil
end
def destroy
log_out
redirect_to root_url
end
end
......@@ -11,6 +11,7 @@ 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
# redirect_to user_url(@user)
......
# The
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Return the current logged-in user (if any).
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
end
# This
class User < ApplicationRecord
before_save { self.email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
......@@ -5,6 +6,13 @@ class User < ApplicationRecord
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: {minimum: 6}
validates :password, presence: true, length: {minimum: 6}
has_secure_password
# 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
end
......@@ -5,7 +5,24 @@
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Login", '#' %></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>
</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>
Rails.application.routes.draw do
root 'static_pages#home'
get '/signup', to: 'users#new'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
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
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
foo:
name: foo
email: foo@bar.com
password_digest: <%= User.digest('password') %>
......@@ -12,6 +12,7 @@ class SiteLayoutTest < ActionDispatch::IntegrationTest
assert_select 'a[href=?]', help_path
assert_select 'a[href=?]', about_path
assert_select 'a[href=?]', contact_path
assert_select 'a[href=?]', login_path
get contact_path
assert_select 'title', full_title('Contact')
......
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
# comment
def setup
@user = users(:foo)
end
# comment
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
# comment
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
......@@ -27,5 +27,6 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
assert_template 'users/show'
assert_not flash.empty?
assert_select 'div.alert'
assert is_logged_in?
end
end
......@@ -7,12 +7,15 @@ require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
# comment
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
include ApplicationHelper
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
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