mirror of
https://github.com/trafficlunar/tomodachi-share.git
synced 2026-06-28 14:44:15 +00:00
feat: ability to update display name and username in profile settings
This commit is contained in:
parent
86c76df873
commit
8b8c9aaa4b
10 changed files with 156 additions and 38 deletions
28
src/app/api/auth/display-name/route.ts
Normal file
28
src/app/api/auth/display-name/route.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { displayNameSchema } from "@/lib/schemas";
|
||||
|
||||
export async function PATCH(request: NextRequest) {
|
||||
const session = await auth();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const { displayName } = await request.json();
|
||||
if (!displayName) return NextResponse.json({ error: "New display name is required" }, { status: 400 });
|
||||
|
||||
const validation = displayNameSchema.safeParse(displayName);
|
||||
if (!validation.success) return NextResponse.json({ error: validation.error.errors[0].message }, { status: 400 });
|
||||
|
||||
try {
|
||||
await prisma.user.update({
|
||||
where: { email: session.user?.email ?? undefined },
|
||||
data: { name: displayName },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Failed to update display name:", error);
|
||||
return NextResponse.json({ error: "Failed to update display name" }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
|
|
@ -1,21 +1,15 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const usernameSchema = z
|
||||
.string()
|
||||
.min(3, "Username must be at least 3 characters long")
|
||||
.max(20, "Username cannot be more than 20 characters long")
|
||||
.regex(/^[a-zA-Z0-9_]+$/, "Username can only contain letters, numbers, and underscores");
|
||||
import { usernameSchema } from "@/lib/schemas";
|
||||
|
||||
export async function PATCH(request: NextRequest) {
|
||||
const session = await auth();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const { username } = await request.json();
|
||||
if (!username) return NextResponse.json({ error: "Username is required" }, { status: 400 });
|
||||
if (!username) return NextResponse.json({ error: "New username is required" }, { status: 400 });
|
||||
|
||||
const validation = usernameSchema.safeParse(username);
|
||||
if (!validation.success) return NextResponse.json({ error: validation.error.errors[0].message }, { status: 400 });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue