Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
feat: improve password validation feedback display
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnygerard committed Nov 10, 2024
1 parent 8986c7a commit 4336e39
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions client/public/icons/error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion client/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class AppComponent {
#sanitizer = inject(DomSanitizer);

constructor() {
for (const name of ["visibility", "visibility_off", "info"]) {
for (const name of ["error", "info", "visibility", "visibility_off"]) {
this.#registerIcon(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
<div [style.left.%]="score() * 25" class="meter-empty"></div>
</div>
</div>
@if (warning()) {
<div class="warning mat-body-medium">
<!-- I'm using the error icon here purely for aesthetic reasons. -->
<mat-icon class="icon" svgIcon="error" />
<span>{{ warning() }}</span>
</div>
}
@if (suggestions().length) {
<ul class="suggestion-list">
@for (suggestion of suggestions(); track $index) {
<li class="suggestion mat-body-medium">
<mat-icon class="suggestion-icon" svgIcon="info" />
<mat-icon class="icon" svgIcon="info" />
<span>{{ suggestion }}</span>
</li>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
background: linear-gradient(to right, #01d5e2, #114ef7);
}

.suggestion {
.suggestion,
.warning {
display: block;

&-list {
Expand All @@ -49,7 +50,7 @@
}
}

.suggestion-icon {
.icon {
$size: 1em;

width: $size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class PasswordStrengthMeterComponent {
isPasswordValid = computed(() => this.score() >= 3);
suggestions = computed(() => this.result().feedback.suggestions);
tooltip = computed(() => this.#tooltipText);
warning = computed(() => this.result().feedback.warning);

get #scoreLabel(): string {
const score = this.score();
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/pipes/password-error.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class PasswordErrorPipe implements PipeTransform {

if (errors["required"]) return "This field is required";
if (errors["maxlength"]) return "Maximum length exceeded";
if (errors["strength"]) return errors["strength"];
if (errors["strength"]) return "Vulnerable password";

throw new Error(`Unexpected validation error: ${JSON.stringify(errors)}`);
}
Expand Down
12 changes: 5 additions & 7 deletions client/src/app/validators/password-validator-factory.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { AbstractControl, ValidationErrors } from "@angular/forms";
import { ZXCVBN_MIN_SCORE } from "_server/constants/password";
import { ZxcvbnResult } from "_server/types/zxcvbn-result";
import { map, Observable, of } from "rxjs";
import { PasswordStrengthService } from "../services/password-strength.service";

const getValidationErrors = (result: ZxcvbnResult): ValidationErrors | null =>
result.score >= ZXCVBN_MIN_SCORE
? null
: { strength: result.feedback.warning || "Vulnerable password" };

export const passwordValidatorFactory = (
passwordStrength: PasswordStrengthService,
...otherControls: AbstractControl[]
Expand All @@ -24,6 +18,10 @@ export const passwordValidatorFactory = (

return passwordStrength
.validate(password, userInputs)
.pipe(map((result) => getValidationErrors(result)));
.pipe(
map((result) =>
result.score >= ZXCVBN_MIN_SCORE ? null : { strength: true },
),
);
};
};

0 comments on commit 4336e39

Please sign in to comment.