Skip to content

Commit c6350fb

Browse files
committed
added back update-profile.ts
1 parent 5e4d443 commit c6350fb

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/actions/update-profile.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// src/actions/update-profile.ts
2+
"use server";
3+
4+
import { headers } from "next/headers";
5+
import { revalidatePath } from "next/cache";
6+
7+
import { auth } from "@/lib/auth";
8+
import { db } from "@/db";
9+
import { users } from "@/db/schema";
10+
import { eq } from "drizzle-orm";
11+
12+
export async function updateProfile(formData: FormData) {
13+
const session = await auth.api.getSession({
14+
headers: await headers(),
15+
});
16+
17+
if (!session?.user) {
18+
throw new Error("Not authenticated");
19+
}
20+
21+
const userId = session.user.id as any;
22+
23+
const name = formData.get("name");
24+
const image = formData.get("image");
25+
26+
if (typeof name !== "string" || name.trim().length < 2) {
27+
throw new Error("Name must be at least 2 characters");
28+
}
29+
30+
await db
31+
.update(users)
32+
.set({
33+
name: name.trim(),
34+
image:
35+
typeof image === "string" && image.trim().length > 0
36+
? image.trim()
37+
: null,
38+
})
39+
.where(eq(users.id, userId));
40+
41+
revalidatePath("/profile");
42+
}

0 commit comments

Comments
 (0)