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.

fix #665 with Zod

Steven Tey0ab42b476048
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index 06a55b0620..605716175f 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -2,8 +2,10 @@ import { withSession } from "@/lib/auth";
import { unsubscribe } from "@/lib/flodesk";
import prisma from "@/lib/prisma";
import { redis } from "@/lib/upstash";
+import { trim } from "@dub/utils";
import cloudinary from "cloudinary";
import { NextResponse } from "next/server";
+import { z } from "zod";
// GET /api/user – get a specific user
export const GET = withSession(async ({ session }) => {
@@ -22,9 +24,17 @@ export const GET = withSession(async ({ session }) => {
});
});
+const updateUserSchema = z.object({
+ name: z.preprocess(trim, z.string().min(1).max(64)).optional(),
+ email: z.preprocess(trim, z.string().email()).optional(),
+ image: z.string().url().optional(),
+});
+
// PUT /api/user – edit a specific user
export const PUT = withSession(async ({ req, session }) => {
- let { name, email, image } = await req.json();
+ let { name, email, image } = await updateUserSchema.parseAsync(
+ await req.json(),
+ );
try {
if (image) {
const { secure_url } = await cloudinary.v2.uploader.upload(image, {
diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/page-client.tsx
index 96ae949b56..aa31a6a132 100644
--- a/apps/web/app/app.dub.co/(dashboard)/settings/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/settings/page-client.tsx
@@ -35,8 +35,8 @@ export default function SettingsPageClient() {
update();
toast.success("Successfully updated your name!");
} else {
- const errorMessage = await res.text();
- toast.error(errorMessage || "Something went wrong");
+ const { error } = await res.json();
+ toast.error(error.message);
}
})
}
@@ -46,6 +46,7 @@ export default function SettingsPageClient() {
description={`This will be the email you use to log in to ${APP_NAME} and receive notifications.`}
inputAttrs={{
name: "email",
+ type: "email",
defaultValue: session?.user?.email || undefined,
placeholder: "[email protected]",
}}
@@ -63,8 +64,8 @@ export default function SettingsPageClient() {
update();
toast.success("Successfully updated your email!");
} else {
- const errorMessage = await res.text();
- toast.error(errorMessage || "Something went wrong");
+ const { error } = await res.json();
+ toast.error(error.message);
}
})
}
diff --git a/packages/ui/src/form.tsx b/packages/ui/src/form.tsx
index cab114c821..682599a541 100644
--- a/packages/ui/src/form.tsx
+++ b/packages/ui/src/form.tsx
@@ -45,7 +45,7 @@ export function Form({
{typeof inputAttrs.defaultValue === "string" ? (
<input
{...inputAttrs}
- type="text"
+ type={inputAttrs.type || "text"}
required
disabled={disabledTooltip ? true : false}
onChange={(e) => setValue(e.target.value)}
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index 7eef42ce4c..7b59effd08 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -14,5 +14,6 @@ export * from "./log";
export * from "./nanoid";
export * from "./nformatter";
export * from "./time-ago";
+export * from "./trim";
export * from "./truncate";
export * from "./urls";
diff --git a/packages/utils/src/functions/trim.ts b/packages/utils/src/functions/trim.ts
new file mode 100644
index 0000000000..7c76098084
--- /dev/null
+++ b/packages/utils/src/functions/trim.ts
@@ -0,0 +1 @@
+export const trim = (u: unknown) => (typeof u === "string" ? u.trim() : u);