Skip to content

Commit

Permalink
Add custom password reset emails.
Browse files Browse the repository at this point in the history
This requires overiding the clearance password resets controller and manually adding clearance routes for full control (recommended by clearance)
  • Loading branch information
phawk committed Sep 14, 2015
1 parent d42beae commit 62a2900
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 13 deletions.
Binary file removed app/controllers/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions app/controllers/reset_passwords_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ResetPasswordsController < Clearance::PasswordsController
def deliver_email(user)
UserMailer.change_password(user).deliver_later
end
end
12 changes: 12 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ def team_invite(team_invite, inviter)
mail to: @user.email, subject: "You have been invited to join a team on Open HQ"
end

def change_password(user)
@user = user
mail(
to: @user.email,
subject: I18n.t(
:change_password,
scope: [:clearance, :models, :clearance_mailer],
default: "Change your password"
)
)
end

end
8 changes: 0 additions & 8 deletions app/views/clearance_mailer/change_password.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/clearance_mailer/change_password.text.erb

This file was deleted.

28 changes: 28 additions & 0 deletions app/views/user_mailer/change_password.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Hi <%= @user.display_name %>,</h1>
<p>You recently requested to reset your password for your Open HQ account. Click the button below to reset it.</p>
<!-- Action -->
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<div>
<!--[if mso]><v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="{{action_url}}" style="height:45px;v-text-anchor:middle;width:200px;" arcsize="7%" stroke="f" fill="t">
<v:fill type="tile" color="#dc4d2f" />
<w:anchorlock/>
<center style="color:#ffffff;font-family:sans-serif;font-size:15px;">Reset your password</center>
</v:roundrect><![endif]-->
<%= link_to "Change my password", edit_user_password_url(@user, token: @user.confirmation_token.html_safe), class: "button button--red" %>
</div>
</td>
</tr>
</table>
<p>If you did not request a password reset, please ignore this email or reply to let us know.</p>
<p>Thanks,<br>The Open HQ Team</p>
<!-- Sub copy -->
<table class="body-sub">
<tr>
<td>
<p class="sub">If you’re having trouble clicking the password reset button, copy and paste the URL below into your web browser.</p>
<p class="sub"><%= edit_user_password_url(@user, token: @user.confirmation_token.html_safe) %></p>
</td>
</tr>
</table>
1 change: 1 addition & 0 deletions config/initializers/clearance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "sign_in_guards/not_deleted_guard"

Clearance.configure do |config|
config.routes = false # Routes managed manually
config.allow_sign_up = false
config.mailer_sender = ENV["MAILGUN_FROM_EMAIL"]
config.sign_in_guards = [SignInGuards::IsTeamMemberGuard, SignInGuards::NotDeletedGuard]
Expand Down
21 changes: 21 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@

Rails.application.routes.draw do

# Clearance routes for authentication
resources :passwords,
controller: 'reset_passwords',
only: [:create, :new]

resource :session,
controller: 'clearance/sessions',
only: [:create]

resources :users,
controller: 'clearance/users',
only: Clearance.configuration.user_actions do
resource :password,
controller: 'clearance/passwords',
only: [:create, :edit, :update]
end

get '/sign_in' => 'clearance/sessions#new', as: 'sign_in'
delete '/sign_out' => 'clearance/sessions#destroy', as: 'sign_out'
# end Clearance routes

constraints(RouteConstraints::RootDomain) do
resources :signups, only: [:new, :create]

Expand Down
4 changes: 4 additions & 0 deletions lib/mailer_previews/user_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ def team_invite
invite = TeamUser.find_by!(status: "invited")
UserMailer.team_invite(invite, User.first)
end

def change_password
UserMailer.change_password(User.first)
end
end

0 comments on commit 62a2900

Please sign in to comment.