Skip to content

Commit

Permalink
Merge pull request awesome-academy#6 from hainv-2854/user_login
Browse files Browse the repository at this point in the history
User Login
  • Loading branch information
vanvtt-0952 authored Jan 7, 2022
2 parents e69b572 + 9ad0686 commit 0d79ec3
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 80 deletions.
3 changes: 2 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ApplicationController < ActionController::Base
before_action :set_locale

protect_from_forgery with: :exception
include SessionsHelper
include Pagy::Backend

private
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/sessions_controller.rb
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&.authenticate params[:session][:password]
log_in user
redirect_to root_url
else
@email = params[:session][:email]
flash.now[:danger] = t ".invalid_email_password"
render :new
end
end

def destroy
log_out
redirect_to root_url
end
end
20 changes: 20 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module SessionsHelper
def log_in user
session[:user_id] = user.id
session[:user_name] = user.name
end

def current_user
@current_user ||= User.find_by id: session[:user_id]
end

def logged_in?
current_user.present?
end

def log_out
session.delete :user_id
session.delete :user_name
@current_user = nil
end
end
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@ class User < ApplicationRecord
has_many :addresses, dependent: :destroy
has_many :orders, dependent: :destroy
has_many :comments, dependent: :destroy

has_secure_password

class << self
def digest string
cost = if ActiveModel::SecurePassword.min_cost
BCrypt::Engine::MIN_COST
else
BCrypt::Engine.cost
end
BCrypt::Password.create string, cost: cost
end
end
end
11 changes: 5 additions & 6 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<link href="https://fonts.googleapis.com/css?family=Lato:300,400,400italic,700,700italic,900,900italic&amp;subset=latin,latin-ext" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open%20Sans:300,400,400italic,600,600italic,700,700italic&amp;subset=latin,latin-ext" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">


<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track": "reload" %>
<%= javascript_pack_tag "application", "data-turbolinks-track": "reload" %>
</head>

<body>
<%= render "shared/header" %>
<%= yield %>
<main id="main">
<div class="container">
<%= yield %>
</div>
</main>
<%= render "shared/footer" %>
</body>
</html>
45 changes: 45 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<% provide :title, t(".title") %>
<div class="page-login">
<div class="wrap-breadcrumb">
<ul>
<li class="item-link"><a href="#" class="link"><%= t ".home" %></a></li>
<li class="item-link"><span><%= t ".title" %></span></li>
</ul>
</div>
<div>
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
</div>
<div class="row">
<div class="col-lg-6 col-sm-6 col-md-6 col-xs-12 col-md-offset-3">
<div class=" main-content-area">
<div class="wrap-login-item ">
<div class="login-form form-item form-stl">
<%= form_for :session, url: login_path, html: {class: "login100-form validate-form"} do |f| %>
<fieldset class="wrap-title">
<h3 class="form-title"><span><%= t ".login_to_account" %></span></h3>
</fieldset>
<fieldset class="wrap-input">
<%= f.label :name, t(".email") %>
<%= f.email_field :email, class: "form-control", value: @email %>
</fieldset>
<fieldset class="wrap-input">
<%= f.label :name, t(".password") %>
<%= f.password_field :password, class: "form-control" %>
</fieldset>
<fieldset class="wrap-input">
<%= f.label :remember_me, class: "remember-field" do %>
<%= f.check_box :remember_me, class: "frm-input" %>
<span><%= t ".remember_me" %></span>
<% end %>
<a class="link-function left-position" href="#" title="<%= t ".forgot_password" %>"><%= t ".forgot_password" %></a>
</fieldset>
<%= f.submit t(".title"), class: "btn btn-primary btn-submit" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
</div>
13 changes: 13 additions & 0 deletions app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
<%= t "the_form_contains" %>
<%= t "errors.error_message", count: pluralize(@user.errors.count, t("errors.error_singular")) %>
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
44 changes: 17 additions & 27 deletions app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@
</div>
<div class="topbar-menu right-menu">
<ul>
<li class="menu-item">
<%= link_to t(".login"), "login_path" %>
</li>
<li class="menu-item">
<%= link_to t(".sign_up"), "signup_path" %>
</li>
<% if logged_in? %>
<li class="menu-item">
<%= t ".welcome"%>
<%= session[:user_name] %>
</li>
<li class="menu-item">
<%= link_to t(".logout"), logout_path, method: :delete %>
</li>
<% else %>
<li class="menu-item">
<%= link_to t(".login"), login_path %>
</li>
<li class="menu-item">
<%= link_to t(".sign_up"), "#" %>
</li>
<% end %>
<li class="menu-item lang-menu">
<%= link_to locale: :en do %>
<span class="img label-before">
Expand Down Expand Up @@ -82,27 +92,7 @@
</ul>
</div>
</div>
<div class="primary-nav-section">
<div class="container">
<ul class="nav primary clone-main-menu" id="mercado_main" data-menuname="Main menu" >
<li class="menu-item home-icon">
<a href="index.html" class="link-term mercado-item-title"><i class="fa fa-home" aria-hidden="true"></i></a>
</li>
<li class="menu-item">
<%= link_to t(".about_us"), "#", class: "link-term mercado-item-title" %>
</li>
<li class="menu-item">
<%= link_to t(".shop"), "#", class: "link-term mercado-item-title" %>
</li>
<li class="menu-item">
<%= link_to t(".cart"), "#", class: "link-term mercado-item-title" %>
</li>
<li class="menu-item">
<%= link_to t(".contact"), "#", class: "link-term mercado-item-title" %>
</li>
</ul>
</div>
</div>
<%= render "shared/navbar" %>
</div>
</div>
</div>
Expand Down
23 changes: 23 additions & 0 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="primary-nav-section">
<div class="container">
<ul class="nav primary clone-main-menu" id="mercado_main" data-menuname="Main menu" >
<li class="menu-item home-icon">
<%= link_to root_path do %>
<i class="fa fa-home" aria-hidden="true"></i>
<% end %>
</li>
<li class="menu-item">
<%= link_to t(".about_us"), "#", class: "link-term mercado-item-title" %>
</li>
<li class="menu-item">
<%= link_to t(".shop"), "#", class: "link-term mercado-item-title" %>
</li>
<li class="menu-item">
<%= link_to t(".cart"), "#", class: "link-term mercado-item-title" %>
</li>
<li class="menu-item">
<%= link_to t(".contact"), "#", class: "link-term mercado-item-title" %>
</li>
</ul>
</div>
</div>
69 changes: 30 additions & 39 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
<main id="main">
<div class="container">
<%= render "shared/banner" %>

<!--BANNER-->
<%= render "shared/banner" %>

<!--On Sale-->
<div class="wrap-show-advance-info-box style-1">
<h3 class="title-box"><%= t ".on_sale" %></h3>
<div class="wrap-top-banner">
<%= link_to "#", class: "link-banner banner-effect-2" do %>
<figure>
<%= image_tag("fashion-accesories-banner.jpg", alt: "", width: "1170", height: "240") %>
</figure>
<% end %>
</div>
</div>
<div class="wrap-show-advance-info-box style-1">
<h3 class="title-box"><%= t ".on_sale" %></h3>
<div class="wrap-top-banner">
<%= link_to "#", class: "link-banner banner-effect-2" do %>
<figure>
<%= image_tag("fashion-accesories-banner.jpg", alt: "", width: "1170", height: "240") %>
</figure>
<% end %>
</div>
</div>

<!--Latest Products-->
<div class="wrap-show-advance-info-box style-1">
<h3 class="title-box"><%= t ".latest_products" %></h3>
<div class="wrap-top-banner">
<%= link_to "#", class: "link-banner banner-effect-2" do %>
<figure>
<%= image_tag("digital-electronic-banner.jpg", alt: "", width: "1170", height: "240") %>
</figure>
<% end %>
</div>
</div>
<div class="wrap-show-advance-info-box style-1">
<h3 class="title-box"><%= t ".latest_products" %></h3>
<div class="wrap-top-banner">
<%= link_to "#", class: "link-banner banner-effect-2" do %>
<figure>
<%= image_tag("digital-electronic-banner.jpg", alt: "", width: "1170", height: "240") %>
</figure>
<% end %>
</div>
</div>

<!--Product Categories-->
<div class="wrap-show-advance-info-box style-1">
<h3 class="title-box"><%= t ".product_categories" %></h3>
<div class="wrap-top-banner">
<%= link_to "#", class: "link-banner banner-effect-2" do %>
<figure>
<%= image_tag("fashion-accesories-banner.jpg", alt: "", width: "1170", height: "240") %>
</figure>
<% end %>
</div>
</div>
<div class="wrap-show-advance-info-box style-1">
<h3 class="title-box"><%= t ".product_categories" %></h3>
<div class="wrap-top-banner">
<%= link_to "#", class: "link-banner banner-effect-2" do %>
<figure>
<%= image_tag("fashion-accesories-banner.jpg", alt: "", width: "1170", height: "240") %>
</figure>
<% end %>
</div>
</main>
</div>
19 changes: 19 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,22 @@ en:
languages:
en: "English"
vi: "Vietnamese"
errors:
error_message: "Please fix the following errors: %{count}"
error_singular: "error"
invalid_email_password_combination: "Invalid email / password combination"
not_activated: "Your account is not activated yet. Please check your email for activation instructions."
sessions:
new:
title: "Login"
new_user: "Create new account"
password: "Password"
login_success: "Login success"
home: "Home"
login_to_account: "Log in to your account"
email: "Email Address"
password: "Password"
remember_me: "Remember me"
forgot_password: "Forgotten password?"
create:
invalid_email_password: "Login fail"
19 changes: 19 additions & 0 deletions config/locales/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,22 @@ vi:
languages:
en: English
vi: "Tiếng Việt"
errors:
error_message: "Vui lòng sửa các lỗi sau: %{count} "
error_singular: "lỗi"
invalid_email_password_combination: "Email / Mật khẩu không hợp lệ"
not_activated: "Tài khoản của bạn chưa được kích hoạt. Vui lòng kiểm tra email của bạn để biết thêm chi tiết."
sessions:
new:
title: "Đăng Nhập"
new_user: "Bạn chưa có tài khoản"
password: "Mật khẩu"
login_success: "Đăng nhập thành công"
home: "Trang chủ"
login_to_account: "Đăng nhập"
email: "Địa chỉ email"
password: "Mật khẩu"
remember_me: "Nhớ mật khẩu"
forgot_password: "Quên mật khẩu?"
create:
invalid_email_password: "Đăng nhập thất bại"
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
scope "(:locale)", locale: /en|vi/ do
root "static_pages#home"
get "static_pages/home"

get "/login", to: "sessions#new"
post "/login", to: "sessions#create"
delete "/logout", to: "sessions#destroy"
end
end
25 changes: 18 additions & 7 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
User.create!(name: "Example User",
email: "[email protected]",
password: "user123",
password_confirmation: "user123",
is_role: 1,
activated: true
)
10.times do |n|
name = Faker::Name.name
email = "example#{n+1}@project.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password,
activated: true
)
end

0 comments on commit 0d79ec3

Please sign in to comment.