mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Implement login, add useUser hook
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import findUserByUsername from '@/services/user/findUserByUsername';
|
||||
import { hashPassword } from '@/config/auth/passwordFns';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import CreateUserValidationSchema from './schema/CreateUserValidationSchema';
|
||||
import GetUserSchema from './schema/GetUserSchema';
|
||||
|
||||
const createNewUser = async ({
|
||||
email,
|
||||
@@ -13,17 +12,8 @@ const createNewUser = async ({
|
||||
dateOfBirth,
|
||||
username,
|
||||
}: z.infer<typeof CreateUserValidationSchema>) => {
|
||||
const userExists = await findUserByUsername(username);
|
||||
|
||||
if (userExists) {
|
||||
throw new ServerError(
|
||||
"Could not register a user with that username as it's already taken.",
|
||||
409,
|
||||
);
|
||||
}
|
||||
|
||||
const hash = await hashPassword(password);
|
||||
const user = DBClient.instance.user.create({
|
||||
const user: z.infer<typeof GetUserSchema> = await DBClient.instance.user.create({
|
||||
data: {
|
||||
username,
|
||||
email,
|
||||
|
||||
13
services/user/findUserByEmail.ts
Normal file
13
services/user/findUserByEmail.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const findUserByEmail = async (email: string) =>
|
||||
DBClient.instance.user.findFirst({
|
||||
where: { email },
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
hash: true,
|
||||
},
|
||||
});
|
||||
|
||||
export default findUserByEmail;
|
||||
23
services/user/findUserById.ts
Normal file
23
services/user/findUserById.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import GetUserSchema from './schema/GetUserSchema';
|
||||
|
||||
const findUserById = async (id: string) => {
|
||||
const user: z.infer<typeof GetUserSchema> | null =
|
||||
await DBClient.instance.user.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
email: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
dateOfBirth: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
export default findUserById;
|
||||
14
services/user/schema/GetUserSchema.ts
Normal file
14
services/user/schema/GetUserSchema.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const GetUserSchema = z.object({
|
||||
id: z.string().uuid(),
|
||||
username: z.string(),
|
||||
createdAt: z.date().or(z.string()),
|
||||
updatedAt: z.date().or(z.string()).optional(),
|
||||
email: z.string().email(),
|
||||
firstName: z.string(),
|
||||
lastName: z.string(),
|
||||
dateOfBirth: z.date().or(z.string()),
|
||||
});
|
||||
|
||||
export default GetUserSchema;
|
||||
18
services/user/schema/LoginValidationSchema.ts
Normal file
18
services/user/schema/LoginValidationSchema.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const LoginValidationSchema = z.object({
|
||||
username: z
|
||||
.string({
|
||||
required_error: 'Username is required.',
|
||||
invalid_type_error: 'Username must be a string.',
|
||||
})
|
||||
.min(1, { message: 'Username is required.' }),
|
||||
password: z
|
||||
.string({
|
||||
required_error: 'Password is required.',
|
||||
invalid_type_error: 'Password must be a string.',
|
||||
})
|
||||
.min(1, { message: 'Password is required.' }),
|
||||
});
|
||||
|
||||
export default LoginValidationSchema;
|
||||
Reference in New Issue
Block a user