-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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, | ||
|
@@ -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"; | ||
|
@@ -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(); | ||
|
@@ -137,7 +140,20 @@ const SignIn = () => { | |
value={password} | ||
onChange={setPassword} | ||
/> | ||
<Button type="submit" isDisabled={isSubmitting} variant="primary"> | ||
{password && passwordState && ( | ||
<PasswordStrength | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more idea:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the library return suggestions if the password is empty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep it does return 2 suggestions There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"> | ||
|
@@ -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 | ||
|
@@ -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)} | ||
> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 It might also be nice to pull the submission handler out into a |
||
Reset password | ||
</Button> | ||
</Form> | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
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