create too expiration_date, modify condition email register

parent 783bd215
Pipeline #969 failed with stages
in 0 seconds
class ConfirmationsController < ApplicationController
def new
@user = Confirmation.new
end
def create
@user = Confirmation.new(email: params[:confirmation][:email].downcase)
user_email = User.find_by(email: params[:confirmation][:email].downcase)
if user_email.blank?
return respond_to { |format| format.js } unless @user.save
ConfirmationMailer.register_email(params[:confirmation][:email].downcase, @user.confirm_token).deliver_later
redirect_to mail_register_path
else
def mail_register
email = params[:confirmation][:email].downcase
if User.find_by(email: email)
flash[:danger] = 'Email existed. Please change !!!'
redirect_to register_path
redirect_to register_step1_path
end
@user = Confirmation.find_or_initialize_by(email: email)
unless @user.save
flash[:danger] = "Email formated invalid"
return redirect_to register_step1_path
end
ConfirmationMailer.register_email(@user).deliver_later
end
end
class UsersController < ApplicationController
before_action :sign_in_validation, only: [:update, :my_page, :my_info]
def my_page
end
......@@ -17,17 +18,22 @@ class UsersController < ApplicationController
end
end
def mail_register
end
def registation
@email = Confirmation.find_by(confirm_token: params[:confirm_token])
@user = User.new
return register_step1_path unless @email
expiration_day = Time.zone.now - @email.updated_at
if expiration_day >= 86400
flash[:danger] = "Link Confirmation is expiration too 24 hours to confirm. Please update your Email again!"
redirect_to register_step1_path
else
@user = User.new
end
end
def create
@user = User.new(sign_up_params)
return respond_to { |format| format.js } unless @user.save
sign_in @user
redirect_to my_page_path
end
......
......@@ -16,6 +16,7 @@ module SessionsHelper
end
def current_user
return if cookies[:remember_token].blank?
remember_token = User.digest(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
......@@ -25,4 +26,5 @@ module SessionsHelper
cookies.delete(:remember_token)
self.current_user = nil
end
end
class ConfirmationMailer < ActionMailer::Base
def register_email(email, confirm_token)
@email = email
@confirm_token = confirm_token
mail(to: @email, subject: 'Welcome To VeNJOB! Confirm Your Email')
def register_email(user)
@user = user
mail(to: user.email, subject: 'Welcome To VeNJOB! Confirm Your Email')
end
end
class Confirmation < ApplicationRecord
before_save { self.email = email.downcase }
before_create :create_confirm_token
before_save :create_confirm_token
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 200 },
format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
......
......@@ -19,7 +19,6 @@ class User < ApplicationRecord
PASSWORD_FORMAT = /\A(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/x
validates :password, format: { with: PASSWORD_FORMAT, message: "is too short or not strength" }
validates :password_confirmation, presence: true
def self.new_remember_token
SecureRandom.urlsafe_base64
......
......@@ -3,10 +3,10 @@
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome <%= @email %></h1>
<h1>Welcome <%= @user.email %></h1>
<p>You're on your way!</p>
<p>Let's confirm your email address.</p>
<p>By clicking on the following link, you are confirming your email address and agreeing to VeNJOB's Terms of Service.</p>
<p><%= link_to 'Confirm Email', registation_url(confirm_token: @confirm_token) %></p>
<p><%= link_to 'Confirm Email', registation_url(confirm_token: @user.confirm_token) %></p>
</body>
</html>
$(".validation").html("<%=escape_javascript render(partial: 'shared/error_messages') %>");
......@@ -4,7 +4,7 @@
<strong>Register</strong>
</div>
<div class="form-register">
<%= form_for(@user, remote: true) do |f| %>
<%= form_for(:confirmation, url: register_step2_path) do |f| %>
<div class="validation"></div>
<div class="text-center email-input">
......
......@@ -9,7 +9,7 @@
<li><%= link_to "Log Out", logout_path , class: "nav-item nav-link", method: "delete" %></li>
<% else %>
<li><%= link_to "Log In", login_path , class: "nav-item nav-link" %></li>
<li><%= link_to "Register", register_path, class: "nav-item nav-link" %></li>
<li><%= link_to "Register", register_step1_path, class: "nav-item nav-link" %></li>
<% end %>
<li><%= link_to "Favorite", '#', class: "nav-item nav-link" %></li>
<li><%= link_to "History", '#', class: "nav-item nav-link" %></li>
......
......@@ -33,7 +33,7 @@ Rails.application.configure do
# config.active_job.queue_adapter = :async
# config.action_mailer.default_options = { from: 'support@venjob.com' }
config.action_mailer.default_options = { from: 'phuoc2811006@gmail.com' }
config.action_mailer.default_options = { from: ENV['GMAIL_USERNAME'] }
config.action_mailer.default_url_options = { host: "localhost:3000" }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
......
......@@ -8,8 +8,8 @@ Rails.application.routes.draw do
get '/login', to: 'sessions#new', as: :login
delete '/logout', to: 'sessions#destroy', as: :logout
get '/register/1', to: 'confirmations#new', as: :register
get '/register/2', to: 'confirmations#mail_register', as: :mail_register
get '/register/1', to: 'confirmations#new', as: :register_step1
post '/register/2', to: 'confirmations#mail_register', as: :register_step2
get '/registation/3code=:confirm_token', to: 'users#registation', as: :registation
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment