Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc1 #6

Merged
merged 19 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enabling user to resend confirmation email if the first one was not r…
…eceived
  • Loading branch information
Stephane Paquet committed Jun 12, 2022
commit 04975aa6bf9efdfdc2ef8855c8f29904568b5464
20 changes: 16 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class UsersController < ApplicationController
def create
@user = User.new(create_user_params)
if @user.save
@user.send_confirmation_email!
redirect_to(root_path, notice: 'Please check your email for confirmation instructions.')
else
render(:new, status: :unprocessable_entity)
Expand Down Expand Up @@ -77,6 +76,22 @@ def update
end
end

def resend_confirmation
@user = User.find(params[:id])

# Make sure the user is entitled to resend the confirmation email
not_owner and return

if @user.confirmed
# Make sure the user is not already confirmed
redirect_to(root_path, notice: 'Your account is already confirmed.')
else
# Otherwise, send the confirmation email
@user.send_confirmation_email!
redirect_to(edit_account_path, notice: 'Please check your email for confirmation instructions.')
end
end

private

def create_user_params
Expand Down Expand Up @@ -114,9 +129,6 @@ def update_password
def user_updates_their_email(msg)
# If the user is updating their email, send a confirmation email and adavise them to check their email
if update_user_params[:email].present? && update_user_params[:email] != @user.email
@user.send_confirmation_email!
@user.confirmed = false
@user.confirmed_at = nil
'Email & account updated successfully. Please check your mail for confirmation instructions.'
else
msg
Expand Down
25 changes: 17 additions & 8 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class User < ApplicationRecord
attr_accessor :current_password

# Callbacks
before_save :downcase_email, if: :will_save_change_to_email?
before_save :generate_password_digest
after_create :send_confirmation_email!
before_save :downcase_email, if: :will_save_change_to_email?
before_save :generate_password_digest

# Mailer configuration
MAILER_FROM_EMAIL = '<Ask Me!> no-reply@ask.me'
Expand Down Expand Up @@ -35,18 +36,18 @@ def confirm!
update_columns(confirmed_at: Time.current, confirmed: true)
end

# Send a confirmation email to the user
def send_confirmation_email!
confirmation_token = signed_id(purpose: :email_confirmation, expires_in: CONFIRMATION_TOKEN_EXPIRATION)
UserMailer.confirmation(self, confirmation_token).deliver_now
end

# Send a password reset email to the user
def send_password_reset_email!
password_reset_token = signed_id(purpose: :reset_password, expires_in: PASSWORD_RESET_TOKEN_EXPIRATION)
UserMailer.password_reset(self, password_reset_token).deliver_now
end

# Send a confirmation email to the user
def send_confirmation_email!
confirmation_token = signed_id(purpose: :email_confirmation, expires_in: CONFIRMATION_TOKEN_EXPIRATION)
UserMailer.confirmation(self, confirmation_token).deliver_now
end

private

# Make sure we save all emails in lowercase
Expand All @@ -59,4 +60,12 @@ def generate_password_digest
require('argon2')
self.password_digest = Argon2::Password.create(password) if password.present?
end

# Method called only when an existing user changes their email address.
def change_email
send_confirmation_email!
self.confirmed = false
self.confirmed_at = nil
end

end
7 changes: 6 additions & 1 deletion app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
<% end %>
<div>
<%= form.label :email, "Current Email" %>
<%= form.email_field :email, autocomplete: "email", class: "form-input w-full block rounded-xl border-slate-300 border-2 focus:outline-none focus:border-green-500 focus:ring-green-500" %>
<%= form.email_field :email, autocomplete: "email", class: "form-input w-full block rounded-xl border-slate-300 border-2 focus:outline-none focus:border-green-500 focus:ring-green-500 mb-1" %>
<% if @user.confirmed %>
<span class="bg-green-300 text-green-700 text-sm px-4 py-1 rounded-full">confirmed</span>
<% else %>
<span class="bg-blue-300 text-blue-700 text-sm px-4 py-1 rounded-full">unconfirmed</span> <%= link_to "resend confirmation", resend_confirmation_path(@user), data: { 'turbo-method': :put }, class: "ml-2 text-blue-700 text-sm hover:text-blue-800 ease-in-out duration-500 transition-colors" %>
<% end %>
</div>
<h2 class="font-bold text-lg my-3">Change your password</h2>
<div>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
put 'account/:id', to: 'users#update', as: 'update_account'
get 'account/:id', to: 'users#edit', as: 'edit_account'
delete 'account/:id', to: 'users#destroy', as: 'destroy_account'
put 'account/:id/resend_confirmation', to: 'users#resend_confirmation', as: 'resend_confirmation'

# User routes
post 'sign_up', to: 'users#create'
Expand Down