Commit ed85a662 by Đường Sỹ Hoàng

Implement basic login

parent c52b154e
......@@ -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/
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
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
......@@ -11,8 +11,9 @@ class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
redirect_to @user
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render "new"
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.nil?
end
# Logs out the current user
def log_out
session.delete( :user_id )
@current_user = nil
end
end
class User < ApplicationRecord
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Constant
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
# Validation macros
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }
validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
before_save { email.downcase! }
before_save { email.downcase! } #Call Backs
has_secure_password
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 "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>
</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>
......@@ -3,7 +3,7 @@
<div class="row">
<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" %>
<%= f.label :name %>
<%= f.text_field :name, class: "form-control" %>
......
Rails.application.routes.draw do
root "static_pages#home"
get "users/new"
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
......@@ -2,8 +2,7 @@ require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get users_new_url
get signup_path
assert_response :success
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
......@@ -19,6 +19,6 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
end
follow_redirect!
assert_template "users/show"
assert_not flash.empty?
assert is_logged_in?
end
end
......@@ -6,5 +6,9 @@ class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
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].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