Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add password requirements UI

Tim Wilson4d5d1e75cb62
diff --git a/apps/web/ui/auth/register/signup-email.tsx b/apps/web/ui/auth/register/signup-email.tsx
index abd8d89633..bbd7a96f30 100644
--- a/apps/web/ui/auth/register/signup-email.tsx
+++ b/apps/web/ui/auth/register/signup-email.tsx
@@ -3,10 +3,11 @@
import { sendOtpAction } from "@/lib/actions/send-otp";
import z from "@/lib/zod";
import { signUpSchema } from "@/lib/zod/schemas/auth";
+import { PasswordRequirements } from "@/ui/shared/password-requirements";
import { Button, Input, useMediaQuery } from "@dub/ui";
import { useAction } from "next-safe-action/hooks";
import { FormEvent, useCallback, useState } from "react";
-import { useForm } from "react-hook-form";
+import { FormProvider, useForm } from "react-hook-form";
import { toast } from "sonner";
import { useRegisterContext } from "./context";
@@ -20,16 +21,18 @@ export const SignUpEmail = () => {
const [showPassword, setShowPassword] = useState(false);
+ const form = useForm<SignUpProps>({
+ defaultValues: {
+ email,
+ },
+ });
+
const {
register,
handleSubmit,
formState: { errors },
getValues,
- } = useForm<SignUpProps>({
- defaultValues: {
- email,
- },
- });
+ } = form;
const { executeAsync, isPending } = useAction(sendOtpAction, {
onSuccess: () => {
@@ -93,6 +96,9 @@ export const SignUpEmail = () => {
error={errors.password?.message}
minLength={8}
/>
+ <FormProvider {...form}>
+ <PasswordRequirements />
+ </FormProvider>
</label>
)}
<Button
diff --git a/apps/web/ui/auth/reset-password-form.tsx b/apps/web/ui/auth/reset-password-form.tsx
index d9a82e47e3..d129b203e8 100644
--- a/apps/web/ui/auth/reset-password-form.tsx
+++ b/apps/web/ui/auth/reset-password-form.tsx
@@ -4,18 +4,21 @@ import z from "@/lib/zod";
import { resetPasswordSchema } from "@/lib/zod/schemas/auth";
import { Button, Input } from "@dub/ui";
import { useParams, useRouter } from "next/navigation";
-import { useForm } from "react-hook-form";
+import { FormProvider, useForm } from "react-hook-form";
import { toast } from "sonner";
+import { PasswordRequirements } from "../shared/password-requirements";
export const ResetPasswordForm = () => {
const router = useRouter();
const { token } = useParams<{ token: string }>();
+ const form = useForm<z.infer<typeof resetPasswordSchema>>();
+
const {
register,
handleSubmit,
formState: { errors, isSubmitting },
- } = useForm<z.infer<typeof resetPasswordSchema>>();
+ } = form;
const onSubmit = handleSubmit(async (data) => {
try {
@@ -56,15 +59,9 @@ export const ResetPasswordForm = () => {
required
autoComplete="new-password"
/>
- {errors.password && (
- <span
- className="block text-sm text-red-500"
- role="alert"
- aria-live="assertive"
- >
- {errors.password.message}
- </span>
- )}
+ <FormProvider {...form}>
+ <PasswordRequirements />
+ </FormProvider>
</label>
<label>
diff --git a/apps/web/ui/shared/password-requirements.tsx b/apps/web/ui/shared/password-requirements.tsx
new file mode 100644
index 0000000000..e3881e6f52
--- /dev/null
+++ b/apps/web/ui/shared/password-requirements.tsx
@@ -0,0 +1,58 @@
+import { CircleCheckFill } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { memo } from "react";
+import { useWatch } from "react-hook-form";
+
+const REQUIREMENTS: {
+ name: string;
+ check: (password: string) => boolean;
+}[] = [
+ {
+ name: "Number",
+ check: (p) => /\d/.test(p),
+ },
+ {
+ name: "Uppercase letter",
+ check: (p) => /[A-Z]/.test(p),
+ },
+ {
+ name: "Lowercase letter",
+ check: (p) => /[a-z]/.test(p),
+ },
+];
+
+export const PasswordRequirements = memo(function PasswordRequirements({
+ field = "password",
+ className,
+}: {
+ field?: string;
+ className?: string;
+}) {
+ const password = useWatch({ name: field });
+
+ return (
+ <div className={cn("mt-2 flex flex-wrap items-center gap-3", className)}>
+ {REQUIREMENTS.map(({ name, check }) => {
+ const checked = password?.length && check(password);
+
+ return (
+ <div
+ key={name}
+ className={cn(
+ "flex items-center gap-1 text-xs text-neutral-400 transition-colors",
+ checked && "text-green-600",
+ )}
+ >
+ <CircleCheckFill
+ className={cn(
+ "size-2.5 transition-opacity",
+ !checked && "opacity-50",
+ )}
+ />
+ <span>{name}</span>
+ </div>
+ );
+ })}
+ </div>
+ );
+});