Skip to content

Commit

Permalink
Merge pull request #36 from khungking909/devise_lockable
Browse files Browse the repository at this point in the history
devise_lockable
  • Loading branch information
vanvtt-0952 authored Apr 8, 2024
2 parents c424782 + cb1604d commit 6e77ca4
Show file tree
Hide file tree
Showing 33 changed files with 612 additions and 346 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ruby "3.2.0"
gem "bootstrap-sass", "3.4.1"
gem "byebug"
gem "connection_pool"
gem "devise", "~> 4.1"
gem "image_processing", ">= 1.2"
gem "jquery-rails"
gem "owlcarousel-rails"
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ GEM
irb (~> 1.10)
reline (>= 0.3.8)
deep_merge (1.2.2)
devise (4.9.3)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
diff-lcs (1.5.1)
docile (1.4.0)
dotenv (3.1.0)
Expand Down Expand Up @@ -178,6 +184,7 @@ GEM
nio4r (2.7.1)
nokogiri (1.16.3-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
owlcarousel-rails (2.2.3.5)
pagy (7.0.11)
psych (5.1.2)
Expand Down Expand Up @@ -236,6 +243,9 @@ GEM
regexp_parser (2.9.0)
reline (0.4.3)
io-console (~> 0.5)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.6)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
Expand Down Expand Up @@ -297,6 +307,8 @@ GEM
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -324,6 +336,7 @@ DEPENDENCIES
connection_pool
cssbundling-rails (~> 1.4)
debug
devise (~> 4.1)
dotenv
dotenv-rails
factory_bot_rails
Expand Down
22 changes: 5 additions & 17 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
# frozen_string_literal: true

class AccountsController < ApplicationController
def new
@account = Account.new
end

def create
@account = Account.new(account_params)
if @account.save
flash[:success] = t("accounts.register_success")
redirect_to(root_path)
else
render(:new, status: :unprocessable_entity)
end
end
class AccountsController < Devise::RegistrationsController
before_action :configure_permitted_parameters, if: :devise_controller?

private
protected

def account_params
params.require(:account).permit(:name, :email, :address, :phone_number, :password, :password_confirmation)
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :name, :address, :phone_number) }
end
end
3 changes: 1 addition & 2 deletions app/controllers/admin/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Admin::AdminController < ApplicationController
def author_admin
return if current_account.admin?

flash[:danger] = t("http_error.forbidden")
redirect_to(login_path)
redirect_to("/403.html")
end
end
6 changes: 3 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ApplicationController < ActionController::Base
include Pagy::Backend
include SessionsHelper
include Devise::Controllers::Helpers

before_action :set_locale

Expand All @@ -17,9 +17,9 @@ def default_url_options
private

def logged_in_user
return if logged_in?
return if account_signed_in?

flash[:danger] = t("sessions.mess_pls_login")
redirect_to(login_path)
redirect_to(new_account_session_path)
end
end
36 changes: 1 addition & 35 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
# frozen_string_literal: true

class SessionsController < ApplicationController
before_action :load_account, :authen_account, only: :create

def new
@account = Account.new
end

def create
log_in(@account)
remember(@account)
flash[:success] = t("sessions.login_success")
redirect_to(root_path)
end

def destroy
log_out
redirect_to(root_url)
end

private

def load_account
@account = Account.find_by(email: params.dig(:session, :email)&.downcase)
return if @account

flash.now[:error] = t("sessions.login_email_err")
render(:new, status: :unprocessable_entity)
end

def authen_account
return if @account.authenticate(params.dig(:session, :password))

flash.now[:error] = t("sessions.login_password_err")
render(:new, status: :unprocessable_entity)
end
class SessionsController < Devise::SessionsController
end
42 changes: 0 additions & 42 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,7 @@
# frozen_string_literal: true

# rubocop:disable Rails/HelperInstanceVariable

module SessionsHelper
def log_in(account)
session[:account_id] = account.id
end

def current_account
if (account_id = session[:account_id])
@current_account ||= Account.find_by(id: account_id)
elsif (account_id = cookies.signed[:account_id])
account = Account.find_by(id: account_id)
if account&.authenticated?(:remember, cookies[:remember_token])
log_in(account)
@current_account = account
end
end
end

def remember(account)
account.remember
cookies.permanent.signed[:account_id] = account.id
cookies.permanent[:remember_token] = account.remember_token
end

def forget(account)
account.forget
cookies.delete(:account_id)
cookies.delete(:remember_token)
end

def log_out
forget(current_account)
reset_session
@current_account = nil
end

def logged_in?
current_account.present?
end

def check_admin
current_account.admin?
end
end

# rubocop:enable Rails/HelperInstanceVariable
52 changes: 4 additions & 48 deletions app/models/account.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,16 @@
# frozen_string_literal: true

class Account < ApplicationRecord
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i.freeze
private_constant :VALID_EMAIL_REGEX

attr_accessor :remember_token

before_save :downcase
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :lockable

enum role: { admin: 1, user: 0 }, _default: :user

has_many :orders, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :products, through: :comments

has_secure_password

validates :name, presence: true, length: { maximum: Settings.DIGIT_50 }
validates :email, presence: true, length: { maximum: Settings.DIGIT_255 },
format: { with: VALID_EMAIL_REGEX }, uniqueness: true
validates :password, presence: true, allow_nil: true

def self.digest(string)
cost = if ActiveModel::SecurePassword.min_cost
BCrypt::Engine::MIN_COST
else
BCrypt::Engine.cost
end
BCrypt::Password.create(string, cost: cost)
end

class << self
def new_token
SecureRandom.urlsafe_base64
end
end

def remember
self.remember_token = Account.new_token
update_column(:remember_digest, Account.digest(remember_token))
end

def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false unless digest

BCrypt::Password.new(digest).is_password?(token)
end

def forget
update_column(:remember_digest, nil)
end

private

def downcase
email.downcase!
end
end
12 changes: 6 additions & 6 deletions app/views/accounts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<%= render "shared/error_messages", object: f.object %>
<%= f.label t "accounts.personal_information" %>
<%= f.text_field :name, placeholder: t("place_holder.name"), class: "glyphicon" %>
<%= f.text_field :address, placeholder: t("place_holder.address"), class: "glyphicon" %>
<%= f.text_field :phone_number, placeholder: t("place_holder.phone_number"), class: "glyphicon" %>
<%= f.text_field :name, placeholder: t("place_holder.name"), class: "w-100" %>
<%= f.text_field :address, placeholder: t("place_holder.address"), class: "w-100" %>
<%= f.text_field :phone_number, placeholder: t("place_holder.phone_number"), class: "w-100" %>

<%= f.label t("accounts.sign_in_information")%>
<%= f.email_field :email, placeholder: t("place_holder.email"), class: "glyphicon" %>
<%= f.password_field :password, placeholder: t("place_holder.password"), class: "glyphicon" %>
<%= f.password_field :password_confirmation, placeholder: t("place_holder.password_confirmation"), class: "glyphicon" %>
<%= f.email_field :email, placeholder: t("place_holder.email"), class: "w-100" %>
<%= f.password_field :password, placeholder: t("place_holder.password"), class: "w-100" %>
<%= f.password_field :password_confirmation, placeholder: t("place_holder.password_confirmation"), class: "w-100" %>
<div class = "col-md-12" >
<%= f.button uppercase(t("back")), class:"form-btn-back", onclick: "window.history.back();" %>
<%= f.submit uppercase(t("accounts.create")), class:"form-btn-submit" %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/accounts/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<% provide :title, t("accounts.sign_up") %>

<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-md-6 offset-md-3">
<h1 class = "sign-up-title"><%= uppercase t("accounts.sign_up") %></h1>
<%= form_for @account do |f| %>
<%= form_for resource, as: resource_name, url: registration_path(resource_name) do |f| %>
<%= render "form", f: f %>
<% end %>
</div>
Expand Down
10 changes: 7 additions & 3 deletions app/views/layouts/_flash.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
<div class="alert alert-danger">
<%= flash[:danger] %>
</div>
<% when flash[:success] %>
<div class="alert alert-success">
<%= flash[:success] %>
<% when flash[:alert] %>
<div class="alert alert-danger">
<%= flash[:alert] %>
</div>
<% when flash[:error] %>
<div class="alert alert-warning">
<%= flash[:error] %>
</div>
<% when flash[:notice] %>
<div class="alert alert-success">
<%= flash[:notice] %>
</div>
<% when flash[:info] %>
<div class="alert alert-dark">
<%= flash[:info] %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<%= render "layouts/flash" %>
<div class="row">
<div class="col-md-4 offset-md-1">
<h2 class = "sign-up-title"><%= uppercase t("accounts.login_customer") %></h2>
<h5><%= t("accounts.login_customer_message") %></h5>
<%= form_for :session, class: "new_account", url: login_path do |f| %>
<%= form_for resource, as: resource_name, url: session_path(resource_name), class: "new_account" do |f| %>
<%= render "layouts/flash" %>
<%= f.email_field :email, placeholder: t("place_holder.email"), class: "glyphicon" %>
<%= f.password_field :password, placeholder: t("place_holder.password"), class: "glyphicon" %>
<div>
Expand All @@ -15,7 +15,7 @@
<div class="col-md-4 offset-md-2">
<h1 class = "sign-up-title"><%= uppercase t("accounts.rediect_login") %></h1>
<h5 style = "margin-bottom: 40px"><%= t("accounts.register_customer_message") %></h5>
<%= link_to new_account_path, class: "btn-redirect" do %>
<%= link_to new_account_registration_path, class: "btn-redirect" do %>
<%= uppercase t "accounts.create"%>
<% end %>

Expand Down
8 changes: 4 additions & 4 deletions app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<% if logged_in? %>
<% if account_signed_in? %>
<li class="nav-item">
<%= link_to t("header.home"), root_path, class: "nav-link" %>
</li>
Expand All @@ -21,18 +21,18 @@
<%= link_to t("header.admin"), admin_products_path, class: "dropdown-item" %>
<% end %>
<%= link_to t("header.order"), orders_path, class: "dropdown-item" %>
<%= link_to t("header.log_out"), logout_path, class: "dropdown-item" %>
<%= link_to t("header.log_out"), destroy_account_session_path, class: "dropdown-item" %>
</div>
</li>
<% else %>
<li class="nav-item"></li>
<%= link_to t("header.home"), root_url, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to t("header.cart"), cart_path, class: "nav-link" %>
<%= link_to t("header.cart"), new_account_session_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to t("header.login"), login_path, class: "nav-link" %>
<%= link_to t("header.login"), new_account_session_path, class: "nav-link" %>
</li>
<% end %>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module DnMinaitei2Ecommerce
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults(7.1)

config.time_zone = "Asia/Ho_Chi_Minh"
# Please, add to the `ignore` list any other `lib` subdirectories that do
# not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example.
Expand Down
Loading

0 comments on commit 6e77ca4

Please sign in to comment.