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

feat: integrate zxcvbn-ts for password strength evaluation #308

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

belhajManel
Copy link
Contributor

What changed?

Resolves #287
Add password strength evaluation during registration and password reset.

2024-12-22-11-17-40.mp4

Why?

Limit account phishing by requiring a minimum password score when creating an account or reseting a password.

How was this change made?

  • Integrated zxcvbn-ts for password strength estimation
  • Created a custom usePasswordStrength hook to use in different components

How was this tested?

👀

Copy link

changeset-bot bot commented Dec 22, 2024

⚠️ No Changeset found

Latest commit: fc44846

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added dependencies Pull requests that update a dependency file frontend CSS, HTML, and JS changes labels Dec 22, 2024
<Banner variant="info">
<p>
<span>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this tiny change because it will cause a hydration error

Copy link
Member

@evadecker evadecker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! This rocks. 🔥🔥🔥

First: Can we tighten up the margin here between the input and strength meter?

CleanShot 2024-12-22 at 10 44 18@2x

Second: seeing this in action, the messages appearing and disappearing while typing is distracting. I wonder if we can update the PasswordStrength component to:

  • Display a circle-help icon to the right of the label when suggestions or a warning exists
  • Open a Popover with the existing banners
  • Popover should display on the right when there's available space, otherwise on the bottom

CleanShot 2024-12-22 at 10 52 11@2x

Third: Can we update the PasswordStrength component to prevent the label from re-animating if the strength does not change?

CleanShot.2024-12-22.at.10.56.39.mp4

Let me know if you have any thoughts/questions/recommended improvements for the design or implementation!

zxcvbnOptions.setOptions(options);
});

export function usePasswordStrength(password: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the creation of a hook for this!

Comment on lines +5 to +19
const loadOptions = async () => {
const zxcvbnCommonPackage = await import(
/* webpackChunkName: "zxcvbnCommonPackage" */ "@zxcvbn-ts/language-common"
);
const zxcvbnEnPackage = await import(
/* webpackChunkName: "zxcvbnEnPackage" */ "@zxcvbn-ts/language-en"
);
return {
dictionary: {
...zxcvbnCommonPackage.dictionary,
...zxcvbnEnPackage.dictionary,
},
translations: zxcvbnEnPackage.translations,
};
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding lazy-loading!

<Button
type="submit"
variant="primary"
isDisabled={isSubmitting || (passwordState && passwordState?.score < 3)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of disabling the button entirely, which can cause accessibility issues, can we instead update our form submission handler to set the error when the score is < 3?

It might also be nice to pull the submission handler out into a handleSubmit function for readability.

Comment on lines +152 to +154
isDisabled={
isSubmitting || (passwordState && passwordState.score < 3)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's display an error instead of disabling the button (see other comment)

@@ -137,7 +140,20 @@ const SignIn = () => {
value={password}
onChange={setPassword}
/>
<Button type="submit" isDisabled={isSubmitting} variant="primary">
{password && passwordState && (
Copy link
Member

@evadecker evadecker Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more idea:

  1. Can we change the api of PasswordStrength to just accept one prop for password, instead of value and warning and suggestions? And then handle all logic for display inside of the component? That way we don't have to check ?? undefined or nest it within a conditional statement wherever it's used, and we can invoke the hook from within the component itself.
  2. Can we display the strength meter even when the password is empty, but hide the label? To prevent reflowing the page when a user begins typing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely!
For the 2nd point when the password is empty do we provide the help icon or is it hidden with the label? It can have initial suggestions for the pwd structure but idk if that really helps?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the library return suggestions if the password is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep it does return 2 suggestions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's try leaving it in even without a label.

@belhajManel
Copy link
Contributor Author

The Popover idea is neat⚡ I'll make the adjustments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file frontend CSS, HTML, and JS changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add password strength requirements during registration and password reset
2 participants