diff --git a/src/app/api/profile/route.ts b/src/app/api/profile/route.ts index 945cc634..e7b030c4 100644 --- a/src/app/api/profile/route.ts +++ b/src/app/api/profile/route.ts @@ -79,18 +79,20 @@ export async function PUT(request: NextRequest) { } // Check if username is taken by another user - const { data: existingUser } = await supabase - .from("profiles") - .select("id") - .eq("username", validationResult.data.username) - .neq("id", user.id) - .single(); - - if (existingUser) { - return NextResponse.json( - { error: "Username is already taken" }, - { status: 400 } - ); + if (validationResult.data.username) { + const { data: existingUser } = await supabase + .from("profiles") + .select("id") + .eq("username", validationResult.data.username) + .neq("id", user.id) + .maybeSingle(); + + if (existingUser) { + return NextResponse.json( + { error: "Username is already taken" }, + { status: 400 } + ); + } } // Get current profile to check for resume changes diff --git a/src/lib/validations.ts b/src/lib/validations.ts index 9b06b8f8..4308caea 100644 --- a/src/lib/validations.ts +++ b/src/lib/validations.ts @@ -80,7 +80,8 @@ export const profileSchema = z.object({ .regex( /^[a-zA-Z0-9_-]+$/, "Username can only contain letters, numbers, underscores, and hyphens" - ), + ) + .optional(), full_name: z.string().max(100).optional().nullable(), bio: z.string().max(1000).optional().nullable(), skills: z.array(z.string()).max(20).default([]),