feat(api): add user search endpoint and normalize response data and validation

This commit is contained in:
Dominik 2025-06-17 21:46:38 +02:00
parent 60c99e0650
commit df4e4e72c1
Signed by: dominik
GPG key ID: 06A4003FC5049644
14 changed files with 574 additions and 368 deletions

View file

@ -1,8 +1,8 @@
'use server';
import type { z } from 'zod';
import type { z } from 'zod/v4';
import bcrypt from 'bcryptjs';
import { disallowedUsernames, registerSchema } from '@/lib/validation/user';
import { registerSchema } from '@/lib/validation/user';
import { prisma } from '@/prisma';
export async function registerAction(data: z.infer<typeof registerSchema>) {
@ -11,39 +11,12 @@ export async function registerAction(data: z.infer<typeof registerSchema>) {
if (!result.success) {
return {
error: result.error.errors[0].message,
error: result.error.issues[0].message,
};
}
const { email, password, firstName, lastName, username } = result.data;
const user = await prisma.user.findUnique({
where: {
email,
},
});
if (user) {
return {
error: 'User already exist with this email',
};
}
const existingUsername = await prisma.user.findUnique({
where: {
name: username,
},
});
if (
existingUsername ||
disallowedUsernames.includes(username.toLowerCase())
) {
return {
error: 'Username already exists',
};
}
const passwordHash = await bcrypt.hash(password, 10);
await prisma.$transaction(async (tx) => {