-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: generate udt verification model to save token Signed-off-by: Miles Zhang <[email protected]> * feat: set mailer for udt verify Signed-off-by: Miles Zhang <[email protected]> * feat: rename udt's contact_info to email Signed-off-by: Miles Zhang <[email protected]> * feat: update udt info and send token business logic Signed-off-by: Miles Zhang <[email protected]> --------- Signed-off-by: Miles Zhang <[email protected]>
- Loading branch information
1 parent
c308dec
commit fa5ef5a
Showing
22 changed files
with
534 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Api | ||
module V1 | ||
class UdtVerificationsController < ApplicationController | ||
before_action :check_udt_info, only: :update | ||
before_action :set_locale, only: :update | ||
|
||
def update | ||
udt_verification = UdtVerification.find_or_create_by(udt_id: @udt.id) | ||
|
||
udt_verification.refresh_token!(request.remote_ip) | ||
UdtVerificationMailer.with(email: @udt.email, token: udt_verification.token, | ||
locale: @locale).send_token.deliver_later | ||
render json: :ok | ||
rescue UdtVerification::TokenSentTooFrequentlyError | ||
raise Api::V1::Exceptions::TokenSentTooFrequentlyError | ||
end | ||
|
||
private | ||
|
||
def check_udt_info | ||
@udt = Udt.find_by(type_hash: params[:id]) | ||
raise Api::V1::Exceptions::UdtNotFoundError if @udt.nil? | ||
raise Api::V1::Exceptions::UdtNoContactEmailError if @udt.email.blank? | ||
end | ||
|
||
def set_locale | ||
@locale = params[:locale] == "zh_CN" ? "zh_CN" : "en" | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class UdtVerificationMailer < ApplicationMailer | ||
default from: "[email protected]" | ||
|
||
def send_token | ||
email = params[:email] | ||
@token = params[:token] | ||
locale = params[:locale] || "en" | ||
I18n.with_locale(locale) do | ||
mail(to: email, subject: "Token Info Verification") | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class UdtVerification < ApplicationRecord | ||
SENT_FREQUENCY_MINUTES = 1 | ||
KEEP_ALIVE_MINUTES = 10 | ||
|
||
class TokenExpiredError < StandardError; end | ||
class TokenNotMatchError < StandardError; end | ||
class TokenSentTooFrequentlyError < StandardError; end | ||
|
||
belongs_to :udt | ||
|
||
def refresh_token!(ip) | ||
raise TokenSentTooFrequentlyError if sent_at.present? && self.sent_at + SENT_FREQUENCY_MINUTES.minutes > Time.now | ||
|
||
self.token = rand(999999).to_s.rjust(6, "0") | ||
self.sent_at = Time.now | ||
self.last_ip = ip | ||
self.save! | ||
end | ||
|
||
def validate_token!(token_params) | ||
raise TokenExpiredError if self.sent_at + KEEP_ALIVE_MINUTES.minutes < Time.now | ||
raise TokenNotMatchError if token != token_params.to_i | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: udt_verifications | ||
# | ||
# id :bigint not null, primary key | ||
# token :integer | ||
# sent_at :datetime | ||
# last_ip :inet | ||
# udt_id :bigint | ||
# udt_type_hash :integer | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_udt_verifications_on_udt_id (udt_id) | ||
# index_udt_verifications_on_udt_type_hash (udt_type_hash) UNIQUE | ||
# |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Dear Token Info Author, | ||
|
||
To ensure the effectiveness of your Token Info modification operation, we have generated a verification code for you to complete the Token Info update. | ||
|
||
Your verification code is: <%= @token %> | ||
|
||
Please note that this verification code is only valid for the next 10 minutes. Please use it to complete your Token Info modification within this time frame and refrain from sharing your code with others. If you have not initiated any action or if this operation is not associated with you, please disregard this email. | ||
|
||
If you have any questions or require assistance, please feel free to contact our customer support team.Thank you for your trust and support! | ||
|
||
Best regards, | ||
The MagicKBase Team |
10 changes: 10 additions & 0 deletions
10
app/views/udt_verification_mailer/send_token.zh_CN.text.erb
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
尊敬的 Token Info 作者, | ||
|
||
为了确保本次 Token Info 修改操作的有效性,我们为您生成了一个验证码,用于完成 Token Info 的修改操作。 | ||
|
||
您的验证码是:<%= @token %> | ||
|
||
请注意,该验证码仅在接下来的10分钟内有效。请在此时间内完成 Token Info 的修改操作,并且请不要分享您的验证码给他人。如果您没有进行任何操作或者不是您的操作,请忽略此邮件。 | ||
|
||
如果您有任何疑问或需要帮助,请随时联系我们的客户支持团队。谢谢您的信任和支持! | ||
MagicKBase 团队 |
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 |
---|---|---|
|
@@ -66,7 +66,19 @@ | |
|
||
# Ignore bad email addresses and do not raise email delivery errors. | ||
# Set this to true and configure the email server for immediate delivery to raise delivery errors. | ||
# config.action_mailer.raise_delivery_errors = false | ||
config.action_mailer.raise_delivery_errors = true | ||
config.action_mailer.delivery_method = :smtp | ||
config.action_mailer.smtp_settings = { | ||
address: ENV["SMTP_ADDRESS"], | ||
port: ENV["SMTP_PORT"], | ||
domain: "[email protected]", | ||
user_name: ENV["SMTP_USER"], | ||
password: ENV["SMTP_PASSWORD"], | ||
authentication: "plain", | ||
enable_starttls_auto: true, | ||
open_timeout: 5, | ||
read_timeout: 5 | ||
} | ||
|
||
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to | ||
# the I18n.default_locale when a translation cannot be found). | ||
|
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
I18n.available_locales = [:en, :zh_CN] | ||
I18n.default_locale = :en |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateUdtVerifications < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :udt_verifications do |t| | ||
t.integer :token | ||
t.datetime :sent_at | ||
t.inet :last_ip | ||
t.belongs_to :udt | ||
t.integer :udt_type_hash | ||
|
||
t.timestamps | ||
t.index :udt_type_hash, unique: true | ||
end | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20230914120928_change_contract_info_to_email_in_udt.rb
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ChangeContractInfoToEmailInUdt < ActiveRecord::Migration[7.0] | ||
def change | ||
rename_column :udts, :contact_info, :email | ||
end | ||
end |
Oops, something went wrong.