feat(api): add user search endpoint and normalize response data and validation
This commit is contained in:
parent
b9dda271af
commit
16a5825dd3
14 changed files with 574 additions and 368 deletions
|
@ -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) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue