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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"@tiptap/extension-typography": "^2.10.3",
"@tiptap/pm": "^2.10.3",
"@tiptap/react": "^2.10.3",
"@zxcvbn-ts/core": "^3.0.4",
"@zxcvbn-ts/language-common": "^3.0.4",
"@zxcvbn-ts/language-en": "^3.0.2",
"convex": "^1.17.3",
"convex-helpers": "^0.1.67",
"lucide-react": "^0.468.0",
Expand Down
32 changes: 32 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/components/app/PasswordStrength/PasswordStrength.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,17 @@ export function PasswordStrength({
)}
</AriaMeter>
{warning && <Banner variant="danger">{warning}</Banner>}
{suggestions && (
{suggestions && suggestions.length > 0 && (
<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

<span>To fix this:</span>
<br />
<ul className="list-disc list-inside">
{suggestions.map((suggestion) => (
<li key={suggestion}>{suggestion}</li>
))}
</ul>
</p>
</span>
</Banner>
)}
</div>
Expand Down
34 changes: 31 additions & 3 deletions src/routes/_unauthenticated/signin.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logo } from "@/components/app";
import { Logo, PasswordStrength } from "@/components/app";
import {
AnimateChangeInHeight,
Banner,
Expand All @@ -14,8 +14,10 @@ import {
Tooltip,
TooltipTrigger,
} from "@/components/common";
import { usePasswordStrength } from "@/utils/usePasswordStrength";
import { useAuthActions } from "@convex-dev/auth/react";
import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router";

import { ConvexError } from "convex/values";
import { ChevronLeft } from "lucide-react";
import { useState } from "react";
Expand All @@ -38,6 +40,7 @@ const SignIn = () => {
const [error, setError] = useState<string | null>(null);
const navigate = useNavigate({ from: "/signin" });
const isClosed = process.env.NODE_ENV === "production";
const passwordState = usePasswordStrength(password);

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
Expand Down Expand Up @@ -137,7 +140,20 @@ const SignIn = () => {
value={password}
onChange={setPassword}
/>
<Button type="submit" isDisabled={isSubmitting} variant="primary">
{password && passwordState && (
<PasswordStrength
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.

value={passwordState.score}
warning={passwordState.feedback.warning ?? undefined}
suggestions={passwordState.feedback.suggestions ?? undefined}
/>
)}
<Button
type="submit"
isDisabled={
isSubmitting || (passwordState && passwordState.score < 3)
}
variant="primary"
Comment on lines +152 to +154
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)

>
{isSubmitting ? "Registering..." : "Register"}
</Button>
<p className="text-sm text-gray-dim text-center text-balance">
Expand Down Expand Up @@ -179,6 +195,7 @@ const ForgotPassword = ({
const [step, setStep] = useState<"forgot" | { email: string }>("forgot");
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const passwordState = usePasswordStrength(newPassword);

return step === "forgot" ? (
<Form
Expand Down Expand Up @@ -278,7 +295,18 @@ const ForgotPassword = ({
value={newPassword}
onChange={setNewPassword}
/>
<Button type="submit" variant="primary" isDisabled={isSubmitting}>
{passwordState && (
<PasswordStrength
value={passwordState.score}
warning={passwordState.feedback.warning || undefined}
suggestions={passwordState.feedback.suggestions || undefined}
/>
)}
<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.

Reset password
</Button>
</Form>
Expand Down
36 changes: 36 additions & 0 deletions src/utils/usePasswordStrength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { type ZxcvbnResult, zxcvbnAsync } from "@zxcvbn-ts/core";
import { zxcvbnOptions } from "@zxcvbn-ts/core";
import { useDeferredValue, useEffect, useState } from "react";

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,
};
};
Comment on lines +5 to +19
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!


loadOptions().then((options) => {
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!

const [result, setResult] = useState<ZxcvbnResult>();
const deferredPassword = useDeferredValue(password);

useEffect(() => {
if (password) {
zxcvbnAsync(deferredPassword).then((response) => setResult(response));
}
}, [password, deferredPassword]);

return result;
}
Loading