Skip to content

Commit d2ce64f

Browse files
committed
[Feature - Reset Password Flow] - added new ENVs, pages, routes, rails and stimulus controllers and mailer. New tests and fixtures added.
1 parent 89f45ce commit d2ce64f

File tree

23 files changed

+431
-1
lines changed

23 files changed

+431
-1
lines changed

app/assets/stylesheets/messages.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,9 @@ img.message__attachment {
593593
inline-size: 1.4em;
594594
}
595595
}
596+
597+
/* Reset password */
598+
.reset-password-alert {
599+
color: var(--color-negative);
600+
visibility: hidden;
601+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Sessions::PasswordResetsController < ApplicationController
2+
allow_unauthenticated_access
3+
4+
before_action :require_smpt
5+
6+
def index
7+
end
8+
def new
9+
end
10+
11+
def show
12+
@password_reset_id = params[:id]
13+
@user = User.find_by_password_reset_id(@password_reset_id)
14+
15+
redirect_to root_url unless @user
16+
end
17+
18+
def update
19+
@user = User.find_by_password_reset_id(password_reset_params[:password_reset_id])
20+
21+
redirect_to root_url unless @user
22+
redirect_to root_url unless password_reset_params[:new_password] == password_reset_params[:confirm_new_password]
23+
24+
@user.update(password: password_reset_params[:new_password])
25+
26+
redirect_to new_session_path
27+
end
28+
29+
def create
30+
email = params[:email_address]
31+
password_reset_url = session_password_reset_url(find_user_by_email(email).password_reset_id)
32+
33+
PasswordResetMailer.with(email: email, url: password_reset_url).password_reset_email.deliver_later
34+
35+
redirect_to new_session_password_reset_path
36+
end
37+
38+
private
39+
40+
def require_smpt
41+
redirect_to root_url unless helpers.smtp_enabled?
42+
end
43+
44+
def find_user_by_email(email)
45+
User.find_by(email_address: email)
46+
end
47+
48+
def password_reset_params
49+
params.require(:user).permit(:new_password, :confirm_new_password, :password_reset_id)
50+
end
51+
end

app/helpers/application_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ def link_back_to(destination)
3535
end
3636
end
3737

38+
def smtp_enabled?
39+
Rails.application.config.feature_enable_smtp.present? &&
40+
Rails.application.config.action_mailer.smtp_settings[:address].present? &&
41+
Rails.application.config.action_mailer.smtp_settings[:port].present? &&
42+
Rails.application.config.action_mailer.smtp_settings[:domain].present? &&
43+
Rails.application.config.action_mailer.smtp_settings[:user_name].present? &&
44+
Rails.application.config.action_mailer.smtp_settings[:password].present?
45+
end
46+
3847
private
3948
def admin_body_class
4049
"admin" if Current.user&.can_administer?
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
static targets = [ "resetPasswordInput", "resetPasswordConfirmInput", "resetPasswordError", "resetPasswordSubmit"]
5+
6+
resetPasswordCheckInputs() {
7+
if(this.#checkResetInputsValues()) {
8+
this.#hideErrorMessage()
9+
this.#enableSubmitButton()
10+
} else {
11+
this.#showErrorMessage()
12+
this.#disableSubmitButton()
13+
}
14+
}
15+
16+
#showErrorMessage() {
17+
this.resetPasswordErrorTarget.style.visibility = "visible"
18+
}
19+
20+
#hideErrorMessage() {
21+
this.resetPasswordErrorTarget.style.visibility = "hidden"
22+
}
23+
24+
#enableSubmitButton() {
25+
this.resetPasswordSubmitTarget.disabled = false
26+
}
27+
28+
#disableSubmitButton() {
29+
this.resetPasswordSubmitTarget.disabled = true
30+
}
31+
32+
#checkResetInputsValues() {
33+
if (this.resetPasswordInputTarget.value.length < 0 || this.resetPasswordConfirmInputTarget.value.length < 0) return false
34+
if (this.resetPasswordInputTarget.value !== this.resetPasswordConfirmInputTarget.value) return false
35+
36+
return true
37+
}
38+
}

app/mailers/application_mailer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ApplicationMailer < ActionMailer::Base
2+
DEFAULT_BASE_FROM="[email protected]"
3+
4+
default from: ENV.fetch("SMTP_INFO_EMAIL_FROM", DEFAULT_BASE_FROM)
5+
layout "mailer"
6+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class PasswordResetMailer < ApplicationMailer
2+
default from: ENV.fetch("SMTP_PASSWORD_RESET_EMAIL_FROM", DEFAULT_BASE_FROM)
3+
4+
def password_reset_email
5+
@email = params[:email]
6+
@url = params[:url]
7+
mail(to: @email, subject: "Campfire Reset Password")
8+
end
9+
end

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class User < ApplicationRecord
2-
include Avatar, Bot, Mentionable, Role, Transferable
2+
include Avatar, Bot, Mentionable, Role, Transferable, Resettable
33

44
has_many :memberships, dependent: :delete_all
55
has_many :rooms, through: :memberships

app/models/user/resettable.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module User::Resettable
2+
extend ActiveSupport::Concern
3+
4+
# RESET_PASSWORD_LINK_EXPIRY_DURATION = 5.minutes
5+
RESET_PASSWORD_LINK_EXPIRY_DURATION = 5.hours
6+
7+
class_methods do
8+
def find_by_password_reset_id(id)
9+
find_signed(id, purpose: :password_reset)
10+
end
11+
end
12+
13+
def password_reset_id
14+
signed_id(purpose: :password_reset, expires_in: RESET_PASSWORD_LINK_EXPIRY_DURATION)
15+
end
16+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>Reset Your Campfire Password</h1>
2+
<p>Hello,</p>
3+
<br>
4+
<p>You have requested a password reset for your Campfire account.</p>
5+
<p>
6+
To reset your Campfire password, please click on this: <%= link_to 'link', @url %>.
7+
</p>
8+
<br>
9+
<p>Campfire</p>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Reset Your Campfire Password
2+
===============================================
3+
Hello,
4+
5+
You have requested a password reset for your Campfire account.
6+
7+
To reset your Campfire password, please click on this: <%= link_to 'link', @url %>.
8+
9+
Campfire

0 commit comments

Comments
 (0)