Skip to content

Commit

Permalink
devise-confirmable
Browse files Browse the repository at this point in the history
  • Loading branch information
khungking909 committed Apr 8, 2024
1 parent cb1604d commit 19af654
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class AccountsController < Devise::RegistrationsController
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :name, :address, :phone_number) }
end

def after_inactive_sign_up_path_for(resource)
new_session_path(resource)
end
end
2 changes: 1 addition & 1 deletion app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Account < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :lockable
:recoverable, :rememberable, :validatable, :lockable, :confirmable

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

Expand Down
13 changes: 13 additions & 0 deletions app/views/accounts/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

<div class="row">
<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<div class="col-md-4 offset-md-4 d-flex flex-column ">
<h2 class="fw-bold mb-3 mt-3"><%= uppercase t("accounts.confirmation_title")%></h2>
<%= render "shared/error_messages", object: resource %>
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, class:"w-100", autocomplete: "email", value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
<%= f.submit uppercase(t("accounts.resend")), class:"form-btn-submit" %>
<%= f.button uppercase(t("back")), class:"form-btn-back", onclick: "window.history.back();" %>
</div>
<% end %>
</div>
5 changes: 5 additions & 0 deletions app/views/accounts/mailer/confirmation_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p><%= t('accounts.registrations.signed_up', email: @email) %></p>

<p><%= t('accounts.registrations.confirm_email') %></p>

<p><%= link_to t('accounts.registrations.confirm_my_account'), confirmation_url(@resource, confirmation_token: @token) %></p>
4 changes: 4 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@
<%= uppercase t "accounts.create"%>
<% end %>

<%= link_to new_account_confirmation_path, class: "btn-redirect ms-3" do %>
<%= uppercase t("accounts.resend")%>
<% end %>

</div>
</div>
11 changes: 11 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@

# Store uploaded files on the local file system in a temporary directory.
config.active_storage.service = :test
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: ENV["HOST"] }
config.action_mailer.smtp_settings = {
address: "sandbox.smtp.mailtrap.io",
port: 2525,
user_name: ENV["USER_EMAIL"],
password: ENV["USER_PASSWORD"],
authentication: "plain",
enable_starttls_auto: true
}

config.action_mailer.perform_caching = false

Expand Down
7 changes: 3 additions & 4 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,14 @@
# without confirming their account.
# Default is 0.days, meaning the user cannot access the website without
# confirming their account.
# config.allow_unconfirmed_access_for = 2.days

config.allow_unconfirmed_access_for = 0.days
# A period that the user is allowed to confirm their account before their
# token becomes invalid. For example, if set to 3.days, the user can confirm
# their account within 3 days after the mail was sent, but on the fourth day
# their account can't be confirmed with the token any more.
# Default is nil, meaning there is no restriction on how long a user can take
# before confirming their account.
# config.confirm_within = 3.days
config.confirm_within = 3.days

# If true, requires any email changes to be confirmed (exactly the same way as
# initial account confirmation) to be applied. Requires additional unconfirmed_email
Expand All @@ -161,7 +160,7 @@
config.reconfirmable = true

# Defines which key will be used when confirming an account
# config.confirmation_keys = [:email]
config.confirmation_keys = [:email]

# ==> Configuration for :rememberable
# The time the user will be remembered without asking for credentials again.
Expand Down
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ en:
personal_information: "Personal Information"
sign_in_information: "Sign-in Information"
create: "CREATE AN ACCOUNT"
resend: "Confirmation"
confirmation_title: "Resend Confirmation"
login_customer: "Customers Login"
login_customer_message: "If you have an account, sign in with your email address."
rediect_login: "New Customers"
register_customer_message: "Creating an account has many benefits: check out faster, keep more than one address, track orders and more."
registrations:
signed_up: "Welcome %{email}!"
confirm_email: "You can confirm your account email through the link below:"
confirm_my_account: "Confirm"
place_holder:
name: "Enter your name *"
email: "Enter your email *"
Expand Down
6 changes: 6 additions & 0 deletions config/locales/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ vi:
personal_information: "Thông tin cá nhân"
sign_in_information: "Thông tin đăng nhập"
create: "TẠO TÀI KHOẢN"
resend: "xác nhận"
confirmation_title: "Gửi Lại Xác Nhận"
login_customer: "Đăng nhập Khách hàng"
login_customer_message: "Nếu bạn đã có tài khoản, hãy đăng nhập bằng địa chỉ email của bạn."
rediect_login: "Khách hàng Mới"
register_customer_message: "Tạo tài khoản mang lại nhiều lợi ích: thanh toán nhanh hơn, lưu nhiều địa chỉ hơn, theo dõi đơn hàng và nhiều hơn nữa."
registrations:
signed_up: "Chào mừng %{email}!"
confirm_email: "Bạn có thể xác nhận tài khoản email của mình thông qua liên kết dưới đây:"
confirm_my_account: "Xác nhận"
place_holder:
name: "Nhập tên của bạn *"
email: "Nhập email của bạn *"
Expand Down
12 changes: 6 additions & 6 deletions db/migrate/20240405025052_devise_create_accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def change
# t.string :last_sign_in_ip

## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable

# Lockable
t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
Expand All @@ -41,7 +41,7 @@ def change

add_index :accounts, :email, unique: true
add_index :accounts, :reset_password_token, unique: true
# add_index :accounts, :confirmation_token, unique: true
# add_index :accounts, :unlock_token, unique: true
add_index :accounts, :confirmation_token, unique: true
add_index :accounts, :unlock_token, unique: true
end
end
6 changes: 6 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/factories/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
address { Faker::Address.full_address }
password { Faker::Internet.password }
password_confirmation { password }
confirmed_at { Time.zone.now }
end
end

0 comments on commit 19af654

Please sign in to comment.