Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove password confirmation field (#11388)
* Remove password confirmation field * Bring initial implementation for PasswordToggler JS class Comes from #9490 Made by @microstudi * Adapt PasswordToggler to redesign and call it * Change the callers of password_field method so they all use the partial * Add spec * Migrate PasswordToggler to vanilla JS * Fix specs * Fix accessibility errors * Refactor and clean-up javascript code * Don't wrap the password label with the input and the button * Fix linter offense * Fix bug when there's an error message * Prevent submission on enter * Show label with required indicator (asterisk and a11y goodies) * Fix button paddings when there's an error * Fix failing spec when not showing help text on failed password reset * Show required password on new session form * Clean-up `password_required?` method Suggested by code review Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro> --------- Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>
- Loading branch information
1 parent
08f506b
commit 1f4e94d
Showing
50 changed files
with
205 additions
and
183 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
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
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
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
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
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,121 @@ | ||
import icon from "src/decidim/redesigned_icon" | ||
|
||
export default class PasswordToggler { | ||
constructor(password) { | ||
this.password = password; | ||
this.input = this.password.querySelector('input[type="password"]'); | ||
this.form = this.input.closest("form"); | ||
this.texts = { | ||
showPassword: this.password.getAttribute("data-show-password") || "Show password", | ||
hidePassword: this.password.getAttribute("data-hide-password") || "Hide password", | ||
hiddenPassword: this.password.getAttribute("data-hidden-password") || "Your password is hidden", | ||
shownPassword: this.password.getAttribute("data-shown-password") || "Your password is shown" | ||
} | ||
this.icons = { | ||
show: icon("eye-line"), | ||
hide: icon("eye-off-line") | ||
} | ||
this.buttonClass = { | ||
onValid: "mt-5", | ||
onInvalid: "mb-2" | ||
} | ||
} | ||
|
||
// Call init() to hide the password confirmation and add a "view password" inline button | ||
init() { | ||
this.createControls(); | ||
this.button.addEventListener("click", (evt) => { | ||
this.toggleVisibiliy(evt); | ||
}); | ||
// to prevent browsers trying to use autocomplete, turn the type back to password before submitting | ||
this.form.addEventListener("submit", () => { | ||
this.hidePassword(); | ||
}); | ||
|
||
// to fix the button margin if there are or not errors | ||
// as foundation abide needs jQuery, we need to do it with 'on' instead of 'addEventListener' | ||
$(this.input).on("invalid.zf.abide", () => { | ||
this.button.classList.remove(this.buttonClass.onValid); | ||
this.button.classList.add(this.buttonClass.onInvalid); | ||
}); | ||
|
||
$(this.input).on("valid.zf.abide", () => { | ||
this.button.classList.add(this.buttonClass.onValid); | ||
this.button.classList.remove(this.buttonClass.onInvalid); | ||
}); | ||
} | ||
|
||
// Call destroy() to switch back to the original password box | ||
destroy() { | ||
this.button.removeEventListener("click"); | ||
this.input.removeEventListener("change"); | ||
this.form.removeEventListener("submit"); | ||
const input = this.input.detach(); | ||
this.inputGroup.replaceWith(input); | ||
} | ||
|
||
createControls() { | ||
this.createButton(); | ||
this.createStatusText(); | ||
this.addInputGroupWrapperAsParent(); | ||
} | ||
|
||
createButton() { | ||
const button = document.createElement("button"); | ||
button.classList.add(this.buttonClass.onValid); | ||
button.setAttribute("type", "button"); | ||
button.setAttribute("aria-controls", this.input.getAttribute("id")); | ||
button.setAttribute("aria-label", this.texts.showPassword); | ||
button.innerHTML = this.icons.show; | ||
this.button = button; | ||
} | ||
|
||
createStatusText() { | ||
const statusText = document.createElement("span"); | ||
statusText.classList.add("sr-only"); | ||
statusText.setAttribute("aria-live", "polite"); | ||
statusText.textContent = this.texts.hiddenPassword; | ||
this.statusText = statusText; | ||
} | ||
|
||
addInputGroupWrapperAsParent() { | ||
const inputGroupWrapper = document.createElement("div"); | ||
inputGroupWrapper.classList.add("filter-search", "filter-container"); | ||
|
||
this.input.parentNode.replaceChild(inputGroupWrapper, this.input); | ||
inputGroupWrapper.appendChild(this.input); | ||
this.input.after(this.button); | ||
|
||
const formError = this.password.querySelector(".form-error"); | ||
if (formError) { | ||
this.input.after(formError); | ||
} | ||
} | ||
|
||
toggleVisibiliy(evt) { | ||
evt.preventDefault(); | ||
if (this.isText()) { | ||
this.hidePassword(); | ||
} else { | ||
this.showPassword(); | ||
} | ||
} | ||
|
||
showPassword() { | ||
this.statusText.textContent = this.texts.shownPassword; | ||
this.button.setAttribute("aria-label", this.texts.hidePassword); | ||
this.button.innerHTML = this.icons.hide; | ||
this.input.setAttribute("type", "text"); | ||
} | ||
|
||
hidePassword() { | ||
this.statusText.textContent = this.texts.hiddenPassword; | ||
this.button.setAttribute("aria-label", this.texts.showPassword); | ||
this.button.innerHTML = this.icons.show; | ||
this.input.setAttribute("type", "password"); | ||
} | ||
|
||
isText() { | ||
return this.input.getAttribute("type") === "text" | ||
} | ||
} |
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
15 changes: 13 additions & 2 deletions
15
decidim-core/app/views/decidim/account/_password_fields.html.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 |
---|---|---|
@@ -1,2 +1,13 @@ | ||
<%= form.password_field :password, password_field_options_for(current_user) %> | ||
<%= form.password_field :password_confirmation, password_field_options_for(current_user).except(:help_text) %> | ||
<div class="user-password" | ||
data-show-password="<%= t "show_password", scope: "decidim.devise.shared.password_fields" %>" | ||
data-hide-password="<%= t "hide_password", scope: "decidim.devise.shared.password_fields" %>" | ||
data-hidden-password="<%= t "hidden_password", scope: "decidim.devise.shared.password_fields" %>" | ||
data-shown-password="<%= t "shown_password", scope: "decidim.devise.shared.password_fields" %>"> | ||
<%= form.send(:custom_label, :password, t("activemodel.attributes.user.password"), { required: true }) %> | ||
<% if local_assigns.has_key?(:show_help_text) || local_assigns.has_key?(:user) %> | ||
<%= form.password_field :password, password_field_options_for(user).merge(value: form.object.password) %> | ||
<% else %> | ||
<%= form.password_field :password, autocomplete: "current-password", required: true, label: false %> | ||
<% end %> | ||
</div> |
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
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
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
Oops, something went wrong.