-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from khungking909/devise_lockable
devise_lockable
- Loading branch information
Showing
33 changed files
with
612 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.