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 requirement validation and some improvements

Kiran K38e186962eae
diff --git a/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx
index 0a316df32c..332f3883d2 100644
--- a/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx
+++ b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx
@@ -2,8 +2,7 @@
import z from "@/lib/zod";
import { resetPasswordSchema } from "@/lib/zod/schemas/auth";
-import { Button } from "@dub/ui";
-import { cn } from "@dub/utils";
+import { Button, Input, Label } from "@dub/ui";
import { zodResolver } from "@hookform/resolvers/zod";
import { useParams, useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
@@ -16,7 +15,7 @@ export const ResetPasswordForm = () => {
const {
register,
handleSubmit,
- formState: { isLoading, disabled, errors },
+ formState: { errors, isSubmitting },
} = useForm<z.infer<typeof resetPasswordSchema>>({
resolver: zodResolver(resetPasswordSchema),
});
@@ -47,29 +46,20 @@ export const ResetPasswordForm = () => {
return (
<>
- <form className="flex flex-col space-y-4" onSubmit={onSubmit}>
+ <form className="flex flex-col space-y-5" onSubmit={onSubmit}>
<input type="hidden" value={token} {...register("token")} />
- <div>
- <label
- htmlFor="password"
- className="mb-2 block text-sm font-medium text-gray-500"
- >
- Password
- </label>
- <input
+
+ <div className="grid w-full max-w-sm items-center gap-2">
+ <Label htmlFor="password">Password</Label>
+ <Input
type="password"
placeholder="********"
{...register("password")}
required
- aria-invalid={errors.password ? "true" : "false"}
- className={cn(
- "w-full max-w-md rounded-md border border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm",
- errors.password && "border-red-500",
- )}
/>
{errors.password && (
<span
- className="mt-2 block text-sm text-red-500"
+ className="block text-sm text-red-500"
role="alert"
aria-live="assertive"
>
@@ -78,26 +68,17 @@ export const ResetPasswordForm = () => {
)}
</div>
- <div>
- <label
- htmlFor="confirmPassword"
- className="mb-2 block text-sm font-medium text-gray-500"
- >
- Confirm Password
- </label>
- <input
+ <div className="grid w-full max-w-sm items-center gap-2">
+ <Label htmlFor="confirmPassword">Confirm Password</Label>
+ <Input
type="password"
placeholder="********"
{...register("confirmPassword")}
required
- className={cn(
- "w-full max-w-md rounded-md border border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm",
- errors.confirmPassword && "border-red-500",
- )}
/>
{errors.confirmPassword && (
<span
- className="mt-2 block text-sm text-red-500"
+ className="block text-sm text-red-500"
role="alert"
aria-live="assertive"
>
@@ -109,8 +90,8 @@ export const ResetPasswordForm = () => {
<Button
text="Reset Password"
type="submit"
- loading={isLoading}
- disabled={disabled}
+ loading={isSubmitting}
+ disabled={isSubmitting}
/>
</form>
</>
diff --git a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx
index 37075b9705..6b2ef21a0e 100644
--- a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx
+++ b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx
@@ -5,6 +5,7 @@ import { signInSchema } from "@/lib/zod/schemas/auth";
import { Button } from "@dub/ui";
import { signIn } from "next-auth/react";
import { useRouter, useSearchParams } from "next/navigation";
+import { useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
@@ -18,18 +19,21 @@ const errorCodes = {
export const SignInWithEmailPassword = () => {
const router = useRouter();
const searchParams = useSearchParams();
+ const [submitting, setSubmitting] = useState(false);
const {
register,
resetField,
handleSubmit,
- formState: { isLoading, isSubmitting },
+ formState: { isLoading },
} = useForm<z.infer<typeof signInSchema>>({
defaultValues: { email: "", password: "" },
});
const onSubmit = handleSubmit(async (data) => {
try {
+ setSubmitting(true);
+
const response = await signIn("credentials", {
...data,
redirect: false,
@@ -51,6 +55,7 @@ export const SignInWithEmailPassword = () => {
return;
}
} catch (error) {
+ setSubmitting(false);
toast.error(error.message);
}
});
@@ -78,7 +83,7 @@ export const SignInWithEmailPassword = () => {
text="Sign in"
type="submit"
loading={isLoading}
- disabled={isSubmitting}
+ disabled={submitting}
/>
</div>
</form>
diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx
index acd3dfec02..e8b120c6d3 100644
--- a/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx
@@ -2,8 +2,7 @@
import z from "@/lib/zod";
import { updatePasswordSchema } from "@/lib/zod/schemas/auth";
-import { Button } from "@dub/ui";
-import { cn } from "@dub/utils";
+import { Button, Input, Label } from "@dub/ui";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
@@ -13,7 +12,9 @@ export const UpdatePassword = () => {
const {
register,
handleSubmit,
- formState: { isLoading, disabled, errors },
+ setError,
+ formState: { isSubmitting, disabled, errors },
+ reset,
} = useForm<z.infer<typeof updatePasswordSchema>>({
resolver: zodResolver(updatePasswordSchema),
});
@@ -30,9 +31,11 @@ export const UpdatePassword = () => {
if (!response.ok) {
const { error } = await response.json();
- throw new Error(error.message);
+ setError("currentPassword", { message: error.message });
+ return;
}
+ reset();
toast.success("Your password has been updated.");
} catch (error) {
toast.error(error.message);
@@ -50,61 +53,46 @@ export const UpdatePassword = () => {
Manage your account password on {process.env.NEXT_PUBLIC_APP_NAME}.
</p>
<div className="flex flex-wrap justify-between space-y-4 sm:space-x-4 sm:space-y-0">
- <div className="min-w-[200px] flex-1">
- <label
- htmlFor="currentPassword"
- className="mb-2 block text-sm font-medium text-gray-500"
- >
- Current Password
- </label>
- <input
+ <div className="grid w-full max-w-sm items-center gap-2">
+ <Label htmlFor="currentPassword">Current Password</Label>
+ <Input
type="password"
placeholder="********"
{...register("currentPassword")}
required
- aria-invalid={errors.currentPassword ? "true" : "false"}
- className={cn(
- "w-full max-w-md rounded-md border border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm",
- errors.currentPassword && "border-red-500",
- )}
/>
- {errors.currentPassword && (
- <span
- className="mt-2 block text-sm text-red-500"
- role="alert"
- aria-live="assertive"
- >
- {errors.currentPassword.message}
- </span>
- )}
</div>
- <div className="min-w-[200px] flex-1">
- <label
- htmlFor="newPassword"
- className="mb-2 block text-sm font-medium text-gray-500"
- >
- New Password
- </label>
- <input
+ <div className="grid w-full max-w-sm items-center gap-2">
+ <Label htmlFor="newPassword">New Password</Label>
+ <Input
type="password"
placeholder="********"
{...register("newPassword")}
required
- className={cn(
- "w-full max-w-md rounded-md border border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm",
- errors.newPassword && "border-red-500",
- )}
/>
- {errors.newPassword && (
- <span
- className="mt-2 block text-sm text-red-500"
- role="alert"
- aria-live="assertive"
- >
- {errors.newPassword.message}
- </span>
- )}
+ </div>
+ </div>
+
+ <div className="flex flex-wrap justify-between">
+ <div className="grid w-full max-w-sm items-center gap-2">
+ <span
+ className="block text-sm text-red-500"
+ role="alert"
+ aria-live="assertive"
+ >
+ {errors.currentPassword?.message}
+ </span>
+ </div>
+
+ <div className="grid w-full max-w-sm items-center gap-2">
+ <span
+ className="block text-sm text-red-500"
+ role="alert"
+ aria-live="assertive"
+ >
+ {errors.newPassword?.message}
+ </span>
</div>
</div>
</div>
@@ -117,7 +105,7 @@ export const UpdatePassword = () => {
<div className="shrink-0">
<Button
text="Update Password"
- loading={isLoading}
+ loading={isSubmitting}
disabled={disabled}
type="submit"
/>
diff --git a/apps/web/lib/auth/password.ts b/apps/web/lib/auth/password.ts
index bb26ba8ec1..122fe8ce41 100644
--- a/apps/web/lib/auth/password.ts
+++ b/apps/web/lib/auth/password.ts
@@ -13,22 +13,3 @@ export async function validatePassword({
}) {
return await compare(password, passwordHash);
}
-
-export const meetsPasswordRequirements = (password: string) => {
- // Check if the password is at least 8 characters long
- if (password.length < 8) {
- return false;
- }
-
- // Check if the password contains at least one uppercase and one lowercase letter
- if (!/[A-Z]/.test(password) || !/[a-z]/.test(password)) {
- return false;
- }
-
- // Check if the password contains at least one digit
- if (!/\d+/.test(password)) {
- return false;
- }
-
- return true;
-};
diff --git a/apps/web/lib/zod/schemas/auth.ts b/apps/web/lib/zod/schemas/auth.ts
index 339955e34f..989e4e5673 100644
--- a/apps/web/lib/zod/schemas/auth.ts
+++ b/apps/web/lib/zod/schemas/auth.ts
@@ -1,15 +1,33 @@
import z from "@/lib/zod";
-export const resetPasswordSchema = z.object({
- token: z.string().min(1),
- password: z.string().min(8, "Password must be at least 8 characters"),
- confirmPassword: z.string(),
-});
+export const passwordSchema = z
+ .string()
+ .min(8, "Password must be at least 8 characters")
+ .regex(
+ /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/,
+ "Password must contain at least one number, one uppercase, and one lowercase letter",
+ );
-export const updatePasswordSchema = z.object({
- currentPassword: z.string().min(1),
- newPassword: z.string().min(8, "Password must be at least 8 characters"),
-});
+export const resetPasswordSchema = z
+ .object({
+ token: z.string().min(1),
+ password: passwordSchema,
+ confirmPassword: z.string(),
+ })
+ .refine((data) => data.password === data.confirmPassword, {
+ message: "Confirm password must match password",
+ path: ["confirmPassword"],
+ });
+
+export const updatePasswordSchema = z
+ .object({
+ currentPassword: z.string().min(1),
+ newPassword: passwordSchema,
+ })
+ .refine((data) => data.currentPassword !== data.newPassword, {
+ message: "New password must be different from current password",
+ path: ["newPassword"],
+ });
export const signInSchema = z.object({
email: z.string().email().min(1),
diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsx
index 00e5f797ea..7b5a5393c0 100644
--- a/packages/ui/src/index.tsx
+++ b/packages/ui/src/index.tsx
@@ -12,6 +12,7 @@ export * from "./date-picker";
export * from "./file-upload";
export * from "./filter";
export * from "./form";
+export * from "./input";
export * from "./input-select";
export * from "./label";
export * from "./modal";
diff --git a/packages/ui/src/input.tsx b/packages/ui/src/input.tsx
new file mode 100644
index 0000000000..79755c76d9
--- /dev/null
+++ b/packages/ui/src/input.tsx
@@ -0,0 +1,38 @@
+import { cn } from "@dub/utils";
+import * as React from "react";
+
+export interface InputProps
+ extends React.InputHTMLAttributes<HTMLInputElement> {
+ error?: string;
+}
+
+const Input = React.forwardRef<HTMLInputElement, InputProps>(
+ ({ className, type, ...props }, ref) => {
+ return (
+ <>
+ <input
+ type={type}
+ className={cn(
+ "w-full max-w-md rounded-md border border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm",
+ className,
+ )}
+ ref={ref}
+ {...props}
+ />
+ {props.error && (
+ <span
+ className="block text-sm text-red-500"
+ role="alert"
+ aria-live="assertive"
+ >
+ {props.error}
+ </span>
+ )}
+ </>
+ );
+ },
+);
+
+Input.displayName = "Input";
+
+export { Input };